1 | /*
|
---|
2 | * Copyright (c) 2005 Martin Decky
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup init
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <fibril.h>
|
---|
37 | #include <stdio.h>
|
---|
38 | #include <stdarg.h>
|
---|
39 | #include <vfs/vfs.h>
|
---|
40 | #include <stdbool.h>
|
---|
41 | #include <errno.h>
|
---|
42 | #include <task.h>
|
---|
43 | #include <stdlib.h>
|
---|
44 | #include <macros.h>
|
---|
45 | #include <str.h>
|
---|
46 | #include <loc.h>
|
---|
47 | #include <str_error.h>
|
---|
48 | #include <config.h>
|
---|
49 | #include <io/logctl.h>
|
---|
50 | #include <vfs/vfs.h>
|
---|
51 | #include <vol.h>
|
---|
52 | #include "untar.h"
|
---|
53 | #include "init.h"
|
---|
54 |
|
---|
55 | #define BANNER_LEFT "######> "
|
---|
56 | #define BANNER_RIGHT " <######"
|
---|
57 |
|
---|
58 | #define ROOT_DEVICE "bd/initrd"
|
---|
59 | #define ROOT_MOUNT_POINT "/"
|
---|
60 |
|
---|
61 | #define LOCFS_FS_TYPE "locfs"
|
---|
62 | #define LOCFS_MOUNT_POINT "/loc"
|
---|
63 |
|
---|
64 | #define TMPFS_FS_TYPE "tmpfs"
|
---|
65 | #define TMPFS_MOUNT_POINT "/tmp"
|
---|
66 |
|
---|
67 | #define SRV_CONSOLE "/srv/hid/console"
|
---|
68 | #define APP_GETTERM "/app/getterm"
|
---|
69 |
|
---|
70 | #define SRV_DISPLAY "/srv/hid/display"
|
---|
71 |
|
---|
72 | #define HID_INPUT "hid/input"
|
---|
73 | #define HID_OUTPUT "hid/output"
|
---|
74 |
|
---|
75 | #define srv_start(path, ...) \
|
---|
76 | srv_startl(path, path, ##__VA_ARGS__, NULL)
|
---|
77 |
|
---|
78 | static const char *sys_dirs[] = {
|
---|
79 | "/w/cfg",
|
---|
80 | "/w/data"
|
---|
81 | };
|
---|
82 |
|
---|
83 | /** Print banner */
|
---|
84 | static void info_print(void)
|
---|
85 | {
|
---|
86 | printf("%s: HelenOS init\n", NAME);
|
---|
87 | }
|
---|
88 |
|
---|
89 | static void oom_check(errno_t rc, const char *path)
|
---|
90 | {
|
---|
91 | if (rc == ENOMEM) {
|
---|
92 | printf("%sOut-of-memory condition detected%s\n", BANNER_LEFT,
|
---|
93 | BANNER_RIGHT);
|
---|
94 | printf("%sBailing out of the boot process after %s%s\n",
|
---|
95 | BANNER_LEFT, path, BANNER_RIGHT);
|
---|
96 | printf("%sMore physical memory is required%s\n", BANNER_LEFT,
|
---|
97 | BANNER_RIGHT);
|
---|
98 | exit(ENOMEM);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | /** Report mount operation success */
|
---|
103 | static bool mount_report(const char *desc, const char *mntpt,
|
---|
104 | const char *fstype, const char *dev, errno_t rc)
|
---|
105 | {
|
---|
106 | switch (rc) {
|
---|
107 | case EOK:
|
---|
108 | if ((dev != NULL) && (str_cmp(dev, "") != 0))
|
---|
109 | printf("%s: %s mounted on %s (%s at %s)\n", NAME, desc, mntpt,
|
---|
110 | fstype, dev);
|
---|
111 | else
|
---|
112 | printf("%s: %s mounted on %s (%s)\n", NAME, desc, mntpt, fstype);
|
---|
113 | break;
|
---|
114 | case EBUSY:
|
---|
115 | printf("%s: %s already mounted on %s\n", NAME, desc, mntpt);
|
---|
116 | return false;
|
---|
117 | case ELIMIT:
|
---|
118 | printf("%s: %s limit exceeded\n", NAME, desc);
|
---|
119 | return false;
|
---|
120 | case ENOENT:
|
---|
121 | printf("%s: %s unknown type (%s)\n", NAME, desc, fstype);
|
---|
122 | return false;
|
---|
123 | default:
|
---|
124 | printf("%s: %s not mounted on %s (%s)\n", NAME, desc, mntpt,
|
---|
125 | str_error(rc));
|
---|
126 | return false;
|
---|
127 | }
|
---|
128 |
|
---|
129 | return true;
|
---|
130 | }
|
---|
131 |
|
---|
132 | /** Mount root file system
|
---|
133 | *
|
---|
134 | * The operation blocks until the root file system
|
---|
135 | * server is ready for mounting.
|
---|
136 | *
|
---|
137 | * @param[in] fstype Root file system type.
|
---|
138 | *
|
---|
139 | * @return True on success.
|
---|
140 | * @return False on failure.
|
---|
141 | *
|
---|
142 | */
|
---|
143 | static bool mount_root(const char *fstype)
|
---|
144 | {
|
---|
145 | const char *root_device = "";
|
---|
146 |
|
---|
147 | if (str_cmp(fstype, "tmpfs") != 0)
|
---|
148 | root_device = ROOT_DEVICE;
|
---|
149 |
|
---|
150 | errno_t rc = vfs_mount_path(ROOT_MOUNT_POINT, fstype, root_device, "",
|
---|
151 | IPC_FLAG_BLOCKING, 0);
|
---|
152 | if (rc == EOK)
|
---|
153 | logctl_set_root();
|
---|
154 |
|
---|
155 | bool ret = mount_report("Root file system", ROOT_MOUNT_POINT, fstype,
|
---|
156 | root_device, rc);
|
---|
157 |
|
---|
158 | rc = vfs_cwd_set(ROOT_MOUNT_POINT);
|
---|
159 | if (rc != EOK) {
|
---|
160 | printf("%s: Unable to set current directory to %s (%s)\n",
|
---|
161 | NAME, ROOT_MOUNT_POINT, str_error(ret));
|
---|
162 | return false;
|
---|
163 | }
|
---|
164 |
|
---|
165 | if ((ret) && (str_cmp(fstype, "tmpfs") == 0)) {
|
---|
166 | printf("%s: Extracting root file system archive\n", NAME);
|
---|
167 | ret = bd_untar(ROOT_DEVICE);
|
---|
168 | }
|
---|
169 |
|
---|
170 | return ret;
|
---|
171 | }
|
---|
172 |
|
---|
173 | /** Mount locfs file system
|
---|
174 | *
|
---|
175 | * The operation blocks until the locfs file system
|
---|
176 | * server is ready for mounting.
|
---|
177 | *
|
---|
178 | * @return True on success.
|
---|
179 | * @return False on failure.
|
---|
180 | *
|
---|
181 | */
|
---|
182 | static bool mount_locfs(void)
|
---|
183 | {
|
---|
184 | errno_t rc = vfs_mount_path(LOCFS_MOUNT_POINT, LOCFS_FS_TYPE, "", "",
|
---|
185 | IPC_FLAG_BLOCKING, 0);
|
---|
186 | return mount_report("Location service file system", LOCFS_MOUNT_POINT,
|
---|
187 | LOCFS_FS_TYPE, NULL, rc);
|
---|
188 | }
|
---|
189 |
|
---|
190 | static errno_t srv_startl(const char *path, ...)
|
---|
191 | {
|
---|
192 | vfs_stat_t s;
|
---|
193 | if (vfs_stat_path(path, &s) != EOK) {
|
---|
194 | printf("%s: Unable to stat %s\n", NAME, path);
|
---|
195 | return ENOENT;
|
---|
196 | }
|
---|
197 |
|
---|
198 | printf("%s: Starting %s\n", NAME, path);
|
---|
199 |
|
---|
200 | va_list ap;
|
---|
201 | const char *arg;
|
---|
202 | int cnt = 0;
|
---|
203 |
|
---|
204 | va_start(ap, path);
|
---|
205 | do {
|
---|
206 | arg = va_arg(ap, const char *);
|
---|
207 | cnt++;
|
---|
208 | } while (arg != NULL);
|
---|
209 | va_end(ap);
|
---|
210 |
|
---|
211 | va_start(ap, path);
|
---|
212 | task_id_t id;
|
---|
213 | task_wait_t wait;
|
---|
214 | errno_t rc = task_spawn(&id, &wait, path, cnt, ap);
|
---|
215 | va_end(ap);
|
---|
216 |
|
---|
217 | if (rc != EOK) {
|
---|
218 | oom_check(rc, path);
|
---|
219 | printf("%s: Error spawning %s (%s)\n", NAME, path,
|
---|
220 | str_error(rc));
|
---|
221 | return rc;
|
---|
222 | }
|
---|
223 |
|
---|
224 | if (!id) {
|
---|
225 | printf("%s: Error spawning %s (invalid task id)\n", NAME,
|
---|
226 | path);
|
---|
227 | return EINVAL;
|
---|
228 | }
|
---|
229 |
|
---|
230 | task_exit_t texit;
|
---|
231 | int retval;
|
---|
232 | rc = task_wait(&wait, &texit, &retval);
|
---|
233 | if (rc != EOK) {
|
---|
234 | printf("%s: Error waiting for %s (%s)\n", NAME, path,
|
---|
235 | str_error(rc));
|
---|
236 | return rc;
|
---|
237 | }
|
---|
238 |
|
---|
239 | if (texit != TASK_EXIT_NORMAL) {
|
---|
240 | printf("%s: Server %s failed to start (unexpectedly "
|
---|
241 | "terminated)\n", NAME, path);
|
---|
242 | return EINVAL;
|
---|
243 | }
|
---|
244 |
|
---|
245 | if (retval != 0)
|
---|
246 | printf("%s: Server %s failed to start (exit code %d)\n", NAME,
|
---|
247 | path, retval);
|
---|
248 |
|
---|
249 | return retval == 0 ? EOK : EPARTY;
|
---|
250 | }
|
---|
251 |
|
---|
252 | static errno_t console(const char *isvc, const char *osvc)
|
---|
253 | {
|
---|
254 | /* Wait for the input service to be ready */
|
---|
255 | service_id_t service_id;
|
---|
256 | errno_t rc = loc_service_get_id(isvc, &service_id, IPC_FLAG_BLOCKING);
|
---|
257 | if (rc != EOK) {
|
---|
258 | printf("%s: Error waiting on %s (%s)\n", NAME, isvc,
|
---|
259 | str_error(rc));
|
---|
260 | return rc;
|
---|
261 | }
|
---|
262 |
|
---|
263 | /* Wait for the output service to be ready */
|
---|
264 | rc = loc_service_get_id(osvc, &service_id, IPC_FLAG_BLOCKING);
|
---|
265 | if (rc != EOK) {
|
---|
266 | printf("%s: Error waiting on %s (%s)\n", NAME, osvc,
|
---|
267 | str_error(rc));
|
---|
268 | return rc;
|
---|
269 | }
|
---|
270 |
|
---|
271 | return srv_start(SRV_CONSOLE, isvc, osvc);
|
---|
272 | }
|
---|
273 |
|
---|
274 | static errno_t display_server(void)
|
---|
275 | {
|
---|
276 | return srv_start(SRV_DISPLAY);
|
---|
277 | }
|
---|
278 |
|
---|
279 | static int app_start(const char *app)
|
---|
280 | {
|
---|
281 | printf("%s: Spawning %s\n", NAME, app);
|
---|
282 |
|
---|
283 | task_id_t id;
|
---|
284 | task_wait_t wait;
|
---|
285 | errno_t rc = task_spawnl(&id, &wait, app, app, NULL);
|
---|
286 | if (rc != EOK) {
|
---|
287 | oom_check(rc, app);
|
---|
288 | printf("%s: Error spawning %s (%s)\n", NAME, app,
|
---|
289 | str_error(rc));
|
---|
290 | return -1;
|
---|
291 | }
|
---|
292 |
|
---|
293 | task_exit_t texit;
|
---|
294 | int retval;
|
---|
295 | rc = task_wait(&wait, &texit, &retval);
|
---|
296 | if ((rc != EOK) || (texit != TASK_EXIT_NORMAL)) {
|
---|
297 | printf("%s: Error retrieving retval from %s (%s)\n", NAME,
|
---|
298 | app, str_error(rc));
|
---|
299 | return rc;
|
---|
300 | }
|
---|
301 |
|
---|
302 | return retval;
|
---|
303 | }
|
---|
304 |
|
---|
305 | static void getterm(const char *svc, const char *app, bool msg)
|
---|
306 | {
|
---|
307 | if (msg) {
|
---|
308 | printf("%s: Spawning %s %s %s --msg --wait -- %s\n", NAME,
|
---|
309 | APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
|
---|
310 |
|
---|
311 | errno_t rc = task_spawnl(NULL, NULL, APP_GETTERM, APP_GETTERM, svc,
|
---|
312 | LOCFS_MOUNT_POINT, "--msg", "--wait", "--", app, NULL);
|
---|
313 | if (rc != EOK) {
|
---|
314 | oom_check(rc, APP_GETTERM);
|
---|
315 | printf("%s: Error spawning %s %s %s --msg --wait -- %s\n",
|
---|
316 | NAME, APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
|
---|
317 | }
|
---|
318 | } else {
|
---|
319 | printf("%s: Spawning %s %s %s --wait -- %s\n", NAME,
|
---|
320 | APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
|
---|
321 |
|
---|
322 | errno_t rc = task_spawnl(NULL, NULL, APP_GETTERM, APP_GETTERM, svc,
|
---|
323 | LOCFS_MOUNT_POINT, "--wait", "--", app, NULL);
|
---|
324 | if (rc != EOK) {
|
---|
325 | oom_check(rc, APP_GETTERM);
|
---|
326 | printf("%s: Error spawning %s %s %s --wait -- %s\n",
|
---|
327 | NAME, APP_GETTERM, svc, LOCFS_MOUNT_POINT, app);
|
---|
328 | }
|
---|
329 | }
|
---|
330 | }
|
---|
331 |
|
---|
332 | static bool mount_tmpfs(void)
|
---|
333 | {
|
---|
334 | errno_t rc = vfs_mount_path(TMPFS_MOUNT_POINT, TMPFS_FS_TYPE, "", "", 0, 0);
|
---|
335 | return mount_report("Temporary file system", TMPFS_MOUNT_POINT,
|
---|
336 | TMPFS_FS_TYPE, NULL, rc);
|
---|
337 | }
|
---|
338 |
|
---|
339 | /** Init system volume.
|
---|
340 | *
|
---|
341 | * See if system volume is configured. If so, try to wait for it to become
|
---|
342 | * available. If not, create basic directories for live image omde.
|
---|
343 | */
|
---|
344 | static errno_t init_sysvol(void)
|
---|
345 | {
|
---|
346 | vol_t *vol = NULL;
|
---|
347 | vol_info_t vinfo;
|
---|
348 | volume_id_t *volume_ids = NULL;
|
---|
349 | size_t nvols;
|
---|
350 | size_t i;
|
---|
351 | errno_t rc;
|
---|
352 | bool found_cfg;
|
---|
353 | const char **cp;
|
---|
354 |
|
---|
355 | rc = vol_create(&vol);
|
---|
356 | if (rc != EOK) {
|
---|
357 | printf("Error contacting volume service.\n");
|
---|
358 | goto error;
|
---|
359 | }
|
---|
360 |
|
---|
361 | rc = vol_get_volumes(vol, &volume_ids, &nvols);
|
---|
362 | if (rc != EOK) {
|
---|
363 | printf("Error getting list of volumes.\n");
|
---|
364 | goto error;
|
---|
365 | }
|
---|
366 |
|
---|
367 | /* XXX This could be handled more efficiently by volsrv itself */
|
---|
368 | found_cfg = false;
|
---|
369 | for (i = 0; i < nvols; i++) {
|
---|
370 | rc = vol_info(vol, volume_ids[i], &vinfo);
|
---|
371 | if (rc != EOK) {
|
---|
372 | printf("Error getting volume information.\n");
|
---|
373 | rc = EIO;
|
---|
374 | goto error;
|
---|
375 | }
|
---|
376 |
|
---|
377 | if (str_cmp(vinfo.path, "/w") == 0) {
|
---|
378 | found_cfg = true;
|
---|
379 | break;
|
---|
380 | }
|
---|
381 | }
|
---|
382 |
|
---|
383 | vol_destroy(vol);
|
---|
384 | free(volume_ids);
|
---|
385 |
|
---|
386 | if (!found_cfg) {
|
---|
387 | /* Prepare directory structure for live image mode */
|
---|
388 | printf("%s: Creating live image directory structure.\n", NAME);
|
---|
389 | cp = sys_dirs;
|
---|
390 | while (*cp != NULL) {
|
---|
391 | rc = vfs_link_path(*cp, KIND_DIRECTORY, NULL);
|
---|
392 | if (rc != EOK) {
|
---|
393 | printf("%s: Error creating directory '%s'.\n",
|
---|
394 | NAME, *cp);
|
---|
395 | return rc;
|
---|
396 | }
|
---|
397 |
|
---|
398 | ++cp;
|
---|
399 | }
|
---|
400 | } else {
|
---|
401 | printf("%s: System volume is configured.\n", NAME);
|
---|
402 | }
|
---|
403 |
|
---|
404 | return EOK;
|
---|
405 | error:
|
---|
406 | vol_destroy(vol);
|
---|
407 | if (volume_ids != NULL)
|
---|
408 | free(volume_ids);
|
---|
409 |
|
---|
410 | return rc;
|
---|
411 | }
|
---|
412 |
|
---|
413 | int main(int argc, char *argv[])
|
---|
414 | {
|
---|
415 | errno_t rc;
|
---|
416 |
|
---|
417 | info_print();
|
---|
418 |
|
---|
419 | if (!mount_root(STRING(RDFMT))) {
|
---|
420 | printf("%s: Exiting\n", NAME);
|
---|
421 | return 1;
|
---|
422 | }
|
---|
423 |
|
---|
424 | /* Make sure file systems are running. */
|
---|
425 | if (str_cmp(STRING(RDFMT), "tmpfs") != 0)
|
---|
426 | srv_start("/srv/fs/tmpfs");
|
---|
427 | if (str_cmp(STRING(RDFMT), "exfat") != 0)
|
---|
428 | srv_start("/srv/fs/exfat");
|
---|
429 | if (str_cmp(STRING(RDFMT), "fat") != 0)
|
---|
430 | srv_start("/srv/fs/fat");
|
---|
431 | srv_start("/srv/fs/cdfs");
|
---|
432 | srv_start("/srv/fs/mfs");
|
---|
433 |
|
---|
434 | srv_start("/srv/klog");
|
---|
435 | srv_start("/srv/fs/locfs");
|
---|
436 | srv_start("/srv/taskmon");
|
---|
437 |
|
---|
438 | if (!mount_locfs()) {
|
---|
439 | printf("%s: Exiting\n", NAME);
|
---|
440 | return 2;
|
---|
441 | }
|
---|
442 |
|
---|
443 | mount_tmpfs();
|
---|
444 |
|
---|
445 | srv_start("/srv/devman");
|
---|
446 | srv_start("/srv/hid/s3c24xx_uart");
|
---|
447 | srv_start("/srv/hid/s3c24xx_ts");
|
---|
448 |
|
---|
449 | srv_start("/srv/bd/vbd");
|
---|
450 | srv_start("/srv/volsrv");
|
---|
451 |
|
---|
452 | srv_start("/srv/net/loopip");
|
---|
453 | srv_start("/srv/net/ethip");
|
---|
454 | srv_start("/srv/net/inetsrv");
|
---|
455 | srv_start("/srv/net/tcp");
|
---|
456 | srv_start("/srv/net/udp");
|
---|
457 | srv_start("/srv/net/dnsrsrv");
|
---|
458 | srv_start("/srv/net/dhcp");
|
---|
459 | srv_start("/srv/net/nconfsrv");
|
---|
460 |
|
---|
461 | srv_start("/srv/clipboard");
|
---|
462 | srv_start("/srv/hid/remcons");
|
---|
463 |
|
---|
464 | srv_start("/srv/hid/input", HID_INPUT);
|
---|
465 | srv_start("/srv/hid/output", HID_OUTPUT);
|
---|
466 | srv_start("/srv/audio/hound");
|
---|
467 |
|
---|
468 | init_sysvol();
|
---|
469 |
|
---|
470 | if (!config_key_exists("console")) {
|
---|
471 | rc = display_server();
|
---|
472 | if (rc == EOK) {
|
---|
473 | app_start("/app/barber");
|
---|
474 | app_start("/app/launcher");
|
---|
475 | app_start("/app/vterm");
|
---|
476 | }
|
---|
477 | }
|
---|
478 |
|
---|
479 | rc = console(HID_INPUT, HID_OUTPUT);
|
---|
480 | if (rc == EOK) {
|
---|
481 | getterm("term/vc0", "/app/bdsh", true);
|
---|
482 | getterm("term/vc1", "/app/bdsh", false);
|
---|
483 | getterm("term/vc2", "/app/bdsh", false);
|
---|
484 | getterm("term/vc3", "/app/bdsh", false);
|
---|
485 | getterm("term/vc4", "/app/bdsh", false);
|
---|
486 | getterm("term/vc5", "/app/bdsh", false);
|
---|
487 | }
|
---|
488 |
|
---|
489 | return 0;
|
---|
490 | }
|
---|
491 |
|
---|
492 | /** @}
|
---|
493 | */
|
---|