summaryrefslogtreecommitdiff
path: root/libdiskfs/boot-parse.c
blob: 15814ef02d77072c27d7909f946bdef8ecf01883 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
   Copyright (C) 1993, 1994 Free Software Foundation

This file is part of the GNU Hurd.

The GNU Hurd is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

The GNU Hurd is distributed in the hope that it will be useful, 
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with the GNU Hurd; see the file COPYING.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */

/* Written by Michael I. Bushnell.  */

#include "priv.h"
#include <stdio.h>
#include <device/device.h>
#include <string.h>
#include <sys/reboot.h>
#include <mach/mig_support.h>

int diskfs_bootflags;
char *diskfs_bootflagarg;

/* Call this if the bootstrap port is null and you want to support
   being a bootstrap filesystem.  ARGC and ARGV should be as passed
   to main.  If the arguments are not in the proper format, an
   error message will be printed on stderr and exit called.  Otherwise,
   diskfs_priv_host, diskfs_master_device, and diskfs_bootflags will be
   set and the Mach kernel name of the bootstrap device will be
   returned.  */
char *
diskfs_parse_bootargs (int argc, char **argv)
{
  char *devname;
  device_t con;
  mach_port_t bootstrap;

  task_get_bootstrap_port (mach_task_self (), &bootstrap);
  if (bootstrap != MACH_PORT_NULL)
    {
      /* We were started not by the kernel, but by the CMU default_pager.
	 It passes us args: -<flags> root_name server_dir_name.  We ignore
	 the last one.  An RPC on our bootstrap port fetches the privileged
	 ports.  */

      struct
	{
	  mach_msg_header_t Head;
	  mach_msg_type_t priv_hostType;
	  mach_port_t priv_host;
	  mach_msg_type_t priv_deviceType;
	  mach_port_t priv_device;
	} msg;
      mach_msg_return_t msg_result;

      static const mach_msg_type_t portCheck = {
	/* msgt_name = */		MACH_MSG_TYPE_MOVE_SEND,
					/* msgt_size = */		32,
					/* msgt_number = */		1,
					/* msgt_inline = */		TRUE,
					/* msgt_longform = */		FALSE,
					/* msgt_deallocate = */		FALSE,
					/* msgt_unused = */		0
      };

      msg.Head.msgh_bits =
	MACH_MSGH_BITS (MACH_MSG_TYPE_MOVE_SEND, MACH_MSG_TYPE_MAKE_SEND_ONCE);
      /* msgh_size passed as argument */
      msg.Head.msgh_remote_port = bootstrap;
      msg.Head.msgh_local_port = mig_get_reply_port ();
      msg.Head.msgh_seqno = 0;
      msg.Head.msgh_id = 999999;

      msg_result = mach_msg (&msg.Head, MACH_SEND_MSG|MACH_RCV_MSG,
			     sizeof msg.Head, sizeof msg,
			     msg.Head.msgh_local_port,
			     MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
      if (msg_result != MACH_MSG_SUCCESS)
	{
	  if ((msg_result == MACH_SEND_INVALID_REPLY) ||
	      (msg_result == MACH_SEND_INVALID_MEMORY) ||
	      (msg_result == MACH_SEND_INVALID_RIGHT) ||
	      (msg_result == MACH_SEND_INVALID_TYPE) ||
	      (msg_result == MACH_SEND_MSG_TOO_SMALL) ||
	      (msg_result == MACH_RCV_INVALID_NAME))
	    mig_dealloc_reply_port (msg.Head.msgh_local_port);
	  else
	    mig_put_reply_port (msg.Head.msgh_local_port);
	  assert (msg_result == MACH_MSG_SUCCESS);
	}
      mig_put_reply_port (msg.Head.msgh_local_port);

      assert (msg.Head.msgh_id == 999999 + 100);
      assert (msg.Head.msgh_size == sizeof msg);
      assert (msg.Head.msgh_bits & MACH_MSGH_BITS_COMPLEX);
      assert (*(int *) &msg.priv_hostType == *(int *) &portCheck);
      assert (*(int *) &msg.priv_deviceType == *(int *) &portCheck);
      diskfs_host_priv = msg.priv_host;
      diskfs_master_device = msg.priv_device;

      /* bootstrap_privileged_ports (bootstrap,
	 &diskfs_host_priv,
	 &diskfs_master_device); */

      /* Clear our bootstrap port, to appear as if run by the kernel.  */
      task_set_bootstrap_port (mach_task_self (), MACH_PORT_NULL);

      devname = argv[2];
    }
  else
    {
      /* The arguments, as passed by the kernel, are as follows:
	 -<flags> hostport deviceport rootname  */

      if (argc != 5 || argv[1][0] != '-')
	{
	  fprintf (stderr,
		   "Usage: %s: -[qsdnx] hostport deviceport rootname\n",
		   program_invocation_name);
	  exit (1);
	}
      diskfs_host_priv = atoi (argv[2]);
      diskfs_master_device = atoi (argv[3]);
      devname = argv[4];
    }

  (void) device_open (diskfs_master_device, D_READ|D_WRITE, "console", &con);
  stderr = stdout = mach_open_devstream (con, "w");
  stdin = mach_open_devstream (con, "r");

  /* For now... */
  /*      readonly = 1; */
      
  /* The possible flags are 
     q  --  RB_ASKNAME
     s  --  RB_SINGLE
     d  --  RB_KDB
     n  --  RB_INITNAME */
  /* q tells us to ask about what device to use, n 
     about what to run as init. */
	
  diskfs_bootflags = 0;
  if (index (argv[1], 'q'))
    diskfs_bootflags |= RB_ASKNAME;
  if (index (argv[1], 's'))
    diskfs_bootflags |= RB_SINGLE;
  if (index (argv[1], 'd'))
    diskfs_bootflags |= RB_KDB;
  if (index (argv[1], 'n'))
    diskfs_bootflags |= RB_INITNAME;
  
  if (diskfs_bootflags & RB_ASKNAME)
    {
      char *tmp;
      printf ("Bootstrap filesystem device name [%s]: ", devname);
      fflush (stdout);
      scanf ("%as\n", &tmp);
      if (*tmp)
	devname = tmp;
    }

  printf ("\nInitial bootstrap: %s", argv[0]);
  fflush (stdout);

  diskfs_bootflagarg = argv[1];

  return devname;
}