source: mainline/uspace/app/sysinst/sysinst.c@ 0cec807a

Last change on this file since 0cec807a was 0cec807a, checked in by Jiri Svoboda <jiri@…>, 7 weeks ago

Unmount destination volume and delete mount point on installation failure.

  • Property mode set to 100644
File size: 33.8 KB
Line 
1/*
2 * Copyright (c) 2025 Jiri Svoboda
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 sysinst
30 * @{
31 */
32/** @file System installer.
33 *
34 * Install the operating system onto a disk device. Note that this only works
35 * on ia32/amd64 with Grub platform 'pc'.
36 */
37
38#include <block.h>
39#include <byteorder.h>
40#include <capa.h>
41#include <errno.h>
42#include <fdisk.h>
43#include <futil.h>
44#include <gfx/render.h>
45#include <io/log.h>
46#include <loc.h>
47#include <stdio.h>
48#include <stdlib.h>
49#include <str.h>
50#include <str_error.h>
51#include <system.h>
52#include <ui/msgdialog.h>
53#include <ui/ui.h>
54#include <ui/window.h>
55#include <vfs/vfs.h>
56#include <vol.h>
57
58#include "grub.h"
59#include "rdimg.h"
60#include "volume.h"
61#include "sysinst.h"
62
63#define NAME "sysinst"
64
65/** Device to install to
66 *
67 * Note that you cannot simply change this, because the installation
68 * device is hardcoded in core.img. If you wanted to install to another
69 * device, you must build your own core.img (e.g. using tools/grub/mkimage.sh
70 * and modifying tools/grub/load.cfg, supplying the device to boot from
71 * in Grub notation).
72 */
73#define DEFAULT_DEV_0 "devices/\\hw\\sys\\00:01.1\\c0d0"
74#define DEFAULT_DEV_1 "devices/\\hw\\sys\\ide1\\c0d0"
75#define DEFAULT_DEV_2 "devices/\\hw\\sys\\00:01.0\\ide1\\c0d0"
76//#define DEFAULT_DEV "devices/\\hw\\pci0\\00:01.2\\uhci_rh\\usb01_a1\\mass-storage0\\l0"
77/** Volume label for the new file system */
78#define INST_VOL_LABEL "HelenOS"
79/** Mount point of system partition when running installed system */
80#define INST_VOL_MP "/w"
81
82#define MOUNT_POINT "/inst"
83
84/** HelenOS live CD volume label */
85#define CD_VOL_LABEL "HelenOS-CD"
86/** XXX Should get this from the volume server */
87#define CD_MOUNT_POINT "/vol/" CD_VOL_LABEL
88
89#define BOOT_FILES_SRC CD_MOUNT_POINT
90#define BOOT_BLOCK_IDX 0 /* MBR */
91
92#define CFG_FILES_SRC "/cfg"
93#define CFG_FILES_DEST MOUNT_POINT "/cfg"
94
95static const char *default_devs[] = {
96 DEFAULT_DEV_0,
97 DEFAULT_DEV_1,
98 DEFAULT_DEV_2,
99 NULL
100};
101
102static const char *sys_dirs[] = {
103 "/cfg",
104 "/data",
105 NULL
106};
107
108static fibril_mutex_t shutdown_lock;
109static fibril_condvar_t shutdown_cv;
110static bool shutdown_stopped;
111static bool shutdown_failed;
112
113static void sysinst_shutdown_complete(void *);
114static void sysinst_shutdown_failed(void *);
115
116static system_cb_t sysinst_system_cb = {
117 .shutdown_complete = sysinst_shutdown_complete,
118 .shutdown_failed = sysinst_shutdown_failed
119};
120
121static void wnd_close(ui_window_t *, void *);
122static errno_t bg_wnd_paint(ui_window_t *, void *);
123
124static ui_window_cb_t bg_window_cb = {
125 .close = wnd_close,
126 .paint = bg_wnd_paint
127};
128
129static ui_window_cb_t progress_window_cb = {
130 .close = wnd_close
131};
132
133static void sysinst_confirm_button(ui_msg_dialog_t *, void *, unsigned);
134static void sysinst_confirm_close(ui_msg_dialog_t *, void *);
135
136static ui_msg_dialog_cb_t sysinst_confirm_cb = {
137 .button = sysinst_confirm_button,
138 .close = sysinst_confirm_close
139};
140
141static errno_t sysinst_restart_dlg_create(sysinst_t *);
142static void sysinst_restart_dlg_button(ui_msg_dialog_t *, void *, unsigned);
143static void sysinst_restart_dlg_close(ui_msg_dialog_t *, void *);
144
145static ui_msg_dialog_cb_t sysinst_restart_dlg_cb = {
146 .button = sysinst_restart_dlg_button,
147 .close = sysinst_restart_dlg_close
148};
149
150static int sysinst_start(sysinst_t *);
151static errno_t sysinst_restart(sysinst_t *);
152static void sysinst_progress_destroy(sysinst_progress_t *);
153static void sysinst_action(sysinst_t *, const char *);
154static void sysinst_error(sysinst_t *, const char *);
155static void sysinst_debug(sysinst_t *, const char *);
156
157static void sysinst_futil_copy_file(void *, const char *, const char *);
158static void sysinst_futil_create_dir(void *, const char *);
159static errno_t sysinst_eject_dev(sysinst_t *, service_id_t);
160
161static futil_cb_t sysinst_futil_cb = {
162 .copy_file = sysinst_futil_copy_file,
163 .create_dir = sysinst_futil_create_dir
164};
165
166static void sysinst_error_msg_button(ui_msg_dialog_t *, void *, unsigned);
167static void sysinst_error_msg_close(ui_msg_dialog_t *, void *);
168
169static ui_msg_dialog_cb_t sysinst_error_msg_cb = {
170 .button = sysinst_error_msg_button,
171 .close = sysinst_error_msg_close
172};
173
174/** Close window.
175 *
176 * @param window Window
177 * @param arg Argument (sysinst_t *)
178 */
179static void wnd_close(ui_window_t *window, void *arg)
180{
181 (void)window;
182 (void)arg;
183}
184
185/** Paint background window.
186 *
187 * @param window Window
188 * @param arg Argument (sysinst_t *)
189 */
190static errno_t bg_wnd_paint(ui_window_t *window, void *arg)
191{
192 sysinst_t *sysinst = (sysinst_t *)arg;
193 gfx_rect_t app_rect;
194 gfx_context_t *gc;
195 errno_t rc;
196
197 gc = ui_window_get_gc(window);
198
199 rc = gfx_set_color(gc, sysinst->bg_color);
200 if (rc != EOK)
201 return rc;
202
203 ui_window_get_app_rect(window, &app_rect);
204
205 rc = gfx_fill_rect(gc, &app_rect);
206 if (rc != EOK)
207 return rc;
208
209 rc = gfx_update(gc);
210 if (rc != EOK)
211 return rc;
212
213 return EOK;
214}
215
216/** Installation confirm dialog OK button press.
217 *
218 * @param dialog Message dialog
219 * @param arg Argument (sysinst_t *)
220 * @param btn Button number
221 * @param earg Entry argument
222 */
223static void sysinst_confirm_button(ui_msg_dialog_t *dialog, void *arg,
224 unsigned btn)
225{
226 sysinst_t *sysinst = (sysinst_t *) arg;
227
228 ui_msg_dialog_destroy(dialog);
229
230 switch (btn) {
231 case 0:
232 /* OK */
233 sysinst_start(sysinst);
234 break;
235 default:
236 /* Cancel */
237 ui_quit(sysinst->ui);
238 break;
239 }
240}
241
242/** Installation confirm dialog close request.
243 *
244 * @param dialog Message dialog
245 * @param arg Argument (sysinst_t *)
246 */
247static void sysinst_confirm_close(ui_msg_dialog_t *dialog, void *arg)
248{
249 sysinst_t *sysinst = (sysinst_t *) arg;
250
251 ui_msg_dialog_destroy(dialog);
252 ui_quit(sysinst->ui);
253}
254
255/** Restart system dialog OK button press.
256 *
257 * @param dialog Message dialog
258 * @param arg Argument (sysinst_t *)
259 * @param btn Button number
260 * @param earg Entry argument
261 */
262static void sysinst_restart_dlg_button(ui_msg_dialog_t *dialog, void *arg,
263 unsigned btn)
264{
265 sysinst_t *sysinst = (sysinst_t *) arg;
266
267 ui_msg_dialog_destroy(dialog);
268
269 (void)sysinst;
270
271 switch (btn) {
272 case 0:
273 /* OK */
274 (void)sysinst_restart(sysinst);
275 break;
276 default:
277 /* Cancel */
278 ui_quit(sysinst->ui);
279 break;
280 }
281}
282
283/** Restat system dialog close request.
284 *
285 * @param dialog Message dialog
286 * @param arg Argument (sysinst_t *)
287 */
288static void sysinst_restart_dlg_close(ui_msg_dialog_t *dialog, void *arg)
289{
290 sysinst_t *sysinst = (sysinst_t *) arg;
291
292 ui_msg_dialog_destroy(dialog);
293 ui_quit(sysinst->ui);
294}
295
296/** Installation error message dialog button press.
297 *
298 * @param dialog Message dialog
299 * @param arg Argument (sysinst_t *)
300 * @param bnum Button number
301 */
302static void sysinst_error_msg_button(ui_msg_dialog_t *dialog,
303 void *arg, unsigned bnum)
304{
305 sysinst_t *sysinst = (sysinst_t *) arg;
306
307 ui_msg_dialog_destroy(dialog);
308 ui_quit(sysinst->ui);
309}
310
311/** Installation error message dialog close request.
312 *
313 * @param dialog Message dialog
314 * @param arg Argument (shutdown_dlg_t *)
315 */
316static void sysinst_error_msg_close(ui_msg_dialog_t *dialog, void *arg)
317{
318 sysinst_t *sysinst = (sysinst_t *) arg;
319
320 ui_msg_dialog_destroy(dialog);
321 ui_quit(sysinst->ui);
322}
323
324/** Create error message dialog.
325 *
326 * @param sysinst System installer
327 * @return EOK on success or an error code
328 */
329static errno_t sysinst_error_msg_create(sysinst_t *sysinst)
330{
331 ui_msg_dialog_params_t params;
332 ui_msg_dialog_t *dialog;
333 errno_t rc;
334
335 ui_msg_dialog_params_init(&params);
336 params.caption = "Error";
337 params.text = sysinst->errmsg;
338 params.flags |= umdf_topmost | umdf_center;
339
340 rc = ui_msg_dialog_create(sysinst->ui, &params, &dialog);
341 if (rc != EOK)
342 return rc;
343
344 ui_msg_dialog_set_cb(dialog, &sysinst_error_msg_cb, (void *)sysinst);
345
346 return EOK;
347}
348
349/** Called when futil is starting to copy a file.
350 *
351 * @param arg Argument (sysinst_t *)
352 * @param src Source path
353 * @param dest Destination path
354 */
355static void sysinst_futil_copy_file(void *arg, const char *src,
356 const char *dest)
357{
358 sysinst_t *sysinst = (sysinst_t *)arg;
359 char buf[128];
360
361 (void)src;
362 snprintf(buf, sizeof(buf), "Copying %s.", dest);
363 sysinst_action(sysinst, buf);
364}
365
366/** Called when futil is about to create a directory.
367 *
368 * @param arg Argument (sysinst_t *)
369 * @param dest Destination path
370 */
371static void sysinst_futil_create_dir(void *arg, const char *dest)
372{
373 sysinst_t *sysinst = (sysinst_t *)arg;
374 char buf[128];
375
376 snprintf(buf, sizeof(buf), "Creating %s.", dest);
377 sysinst_action(sysinst, buf);
378}
379
380/** System shutdown complete.
381 *
382 * @param arg Argument (shutdown_t *)
383 */
384static void sysinst_shutdown_complete(void *arg)
385{
386 (void)arg;
387
388 fibril_mutex_lock(&shutdown_lock);
389 shutdown_stopped = true;
390 shutdown_failed = false;
391 fibril_condvar_broadcast(&shutdown_cv);
392 fibril_mutex_unlock(&shutdown_lock);
393}
394
395/** System shutdown failed.
396 *
397 * @param arg Argument (not used)
398 */
399static void sysinst_shutdown_failed(void *arg)
400{
401 (void)arg;
402
403 fibril_mutex_lock(&shutdown_lock);
404 shutdown_stopped = true;
405 shutdown_failed = true;
406 fibril_condvar_broadcast(&shutdown_cv);
407 fibril_mutex_unlock(&shutdown_lock);
408}
409
410/** Check the if the destination device exists.
411 *
412 * @param dev Disk device
413 *
414 * @return EOK on success or an error code
415 */
416static errno_t sysinst_check_dev(const char *dev)
417{
418 service_id_t sid;
419 errno_t rc;
420
421 rc = loc_service_get_id(dev, &sid, 0);
422 if (rc != EOK)
423 return rc;
424
425 (void)sid;
426 return EOK;
427}
428
429/** Label and mount the destination device.
430 *
431 * @param sysinst System installer
432 * @param dev Disk device to label
433 * @param psvc_id Place to store service ID of the created partition
434 *
435 * @return EOK on success or an error code
436 */
437static errno_t sysinst_label_dev(sysinst_t *sysinst, const char *dev)
438{
439 fdisk_t *fdisk = NULL;
440 fdisk_dev_t *fdev = NULL;
441 fdisk_part_t *part;
442 fdisk_part_spec_t pspec;
443 fdisk_part_info_t pinfo;
444 bool dir_created = false;
445 bool label_created = false;
446 capa_spec_t capa;
447 service_id_t sid;
448 errno_t rc;
449
450 sysinst_debug(sysinst, "sysinst_label_dev(): get service ID");
451
452 rc = loc_service_get_id(dev, &sid, 0);
453 if (rc != EOK)
454 goto error;
455
456 sysinst_debug(sysinst, "sysinst_label_dev(): open device");
457
458 rc = fdisk_create(&fdisk);
459 if (rc != EOK) {
460 sysinst_error(sysinst, "Error initializing fdisk.");
461 goto error;
462 }
463
464 rc = fdisk_dev_open(fdisk, sid, &fdev);
465 if (rc != EOK) {
466 sysinst_error(sysinst, "Error opening device.");
467 goto error;
468 }
469
470 sysinst_debug(sysinst, "sysinst_label_dev(): create mount directory");
471
472 rc = vfs_link_path(MOUNT_POINT, KIND_DIRECTORY, NULL);
473 if (rc != EOK) {
474 sysinst_error(sysinst, "Error creating mount directory.");
475 goto error;
476 }
477
478 dir_created = true;
479
480 sysinst_debug(sysinst, "sysinst_label_dev(): create label");
481
482 rc = fdisk_label_create(fdev, lt_mbr);
483 if (rc != EOK) {
484 sysinst_error(sysinst, "Error creating label.");
485 goto error;
486 }
487
488 label_created = true;
489
490 sysinst_debug(sysinst, "sysinst_label_dev(): create partition");
491
492 rc = fdisk_part_get_max_avail(fdev, spc_pri, &capa);
493 if (rc != EOK) {
494 sysinst_error(sysinst,
495 "Error getting available capacity.");
496 goto error;
497 }
498
499 fdisk_pspec_init(&pspec);
500 pspec.capacity = capa;
501 pspec.pkind = lpk_primary;
502 pspec.fstype = fs_ext4; /* Cannot be changed without modifying core.img */
503 pspec.mountp = MOUNT_POINT;
504 pspec.label = INST_VOL_LABEL;
505
506 rc = fdisk_part_create(fdev, &pspec, &part);
507 if (rc != EOK) {
508 sysinst_error(sysinst, "Error creating partition.");
509 goto error;
510 }
511
512 rc = fdisk_part_get_info(part, &pinfo);
513 if (rc != EOK) {
514 sysinst_error(sysinst, "Error getting partition information.");
515 goto error;
516 }
517
518 sysinst_debug(sysinst, "sysinst_label_dev(): OK");
519 fdisk_dev_close(fdev);
520 fdisk_destroy(fdisk);
521 sysinst->psvc_id = pinfo.svc_id;
522 return EOK;
523error:
524 if (label_created)
525 fdisk_label_destroy(fdev);
526 if (dir_created)
527 (void)vfs_unlink_path(MOUNT_POINT);
528 if (fdev != NULL)
529 fdisk_dev_close(fdev);
530 if (fdisk != NULL)
531 fdisk_destroy(fdisk);
532 return rc;
533}
534
535/** Finish/unmount destination device.
536 *
537 * @param sysinst System installer
538 *
539 * @return EOK on success or an error code
540 */
541static errno_t sysinst_finish_dev(sysinst_t *sysinst)
542{
543 errno_t rc;
544
545 sysinst_debug(sysinst, "sysinst_finish_dev(): eject target volume");
546 rc = sysinst_eject_dev(sysinst, sysinst->psvc_id);
547 if (rc != EOK)
548 return rc;
549
550 sysinst_debug(sysinst, "sysinst_finish_dev(): "
551 "deleting mount directory");
552 (void)vfs_unlink_path(MOUNT_POINT);
553
554 return EOK;
555}
556
557/** Set up system volume structure.
558 *
559 * @param sysinst System installer
560 * @return EOK on success or an error code
561 */
562static errno_t sysinst_setup_sysvol(sysinst_t *sysinst)
563{
564 errno_t rc;
565 char *path = NULL;
566 const char **cp;
567 int rv;
568
569 cp = sys_dirs;
570 while (*cp != NULL) {
571 rv = asprintf(&path, "%s%s", MOUNT_POINT, *cp);
572 if (rv < 0) {
573 rc = ENOMEM;
574 goto error;
575 }
576
577 rc = vfs_link_path(path, KIND_DIRECTORY, NULL);
578 if (rc != EOK) {
579 sysinst_error(sysinst, "Error creating directory.");
580 goto error;
581 }
582
583 free(path);
584 path = NULL;
585 ++cp;
586 }
587
588 free(path);
589 path = NULL;
590
591 /* Copy initial configuration files */
592 rc = futil_rcopy_contents(sysinst->futil, CFG_FILES_SRC,
593 CFG_FILES_DEST);
594 if (rc != EOK) {
595 sysinst_error(sysinst, "Error copying initial configuration "
596 "files.");
597 return rc;
598 }
599
600 return EOK;
601error:
602 if (path != NULL)
603 free(path);
604 return rc;
605}
606
607/** Copy boot files.
608 *
609 * @return EOK on success or an error code
610 */
611static errno_t sysinst_copy_boot_files(sysinst_t *sysinst)
612{
613 errno_t rc;
614
615 log_msg(LOG_DEFAULT, LVL_NOTE,
616 "sysinst_copy_boot_files(): copy bootloader files");
617 rc = futil_rcopy_contents(sysinst->futil, BOOT_FILES_SRC, MOUNT_POINT);
618 if (rc != EOK) {
619 sysinst_error(sysinst, "Error copying bootloader "
620 "files.");
621 return rc;
622 }
623
624 sysinst_debug(sysinst, "sysinst_copy_boot_files(): OK");
625 return EOK;
626}
627
628/** Set up configuration in the initial RAM disk.
629 *
630 * @param sysinst System installer
631 * @return EOK on success or an error code
632 */
633static errno_t sysinst_customize_initrd(sysinst_t *sysinst)
634{
635 errno_t rc;
636 rd_img_t *rd = NULL;
637 char *rdpath = NULL;
638 char *path = NULL;
639 vol_volumes_t *volumes = NULL;
640 vol_volume_t *volume = NULL;
641 int rv;
642
643 rc = rd_img_open(MOUNT_POINT "/boot/initrd.img", &rdpath, &rd);
644 if (rc != EOK) {
645 sysinst_error(sysinst, "Error opening initial RAM disk image.");
646 goto error;
647 }
648
649 rv = asprintf(&path, "%s%s", rdpath, "/cfg/initvol.sif");
650 if (rv < 0) {
651 rc = ENOMEM;
652 goto error;
653 }
654
655 sysinst_debug(sysinst, "Configuring volume server.");
656
657 rc = vol_volumes_create(path, &volumes);
658 if (rc != EOK) {
659 sysinst_error(sysinst,
660 "Error creating volume server configuration.");
661 rc = EIO;
662 goto error;
663 }
664
665 sysinst_debug(sysinst, "Configuring volume server: look up volume");
666 rc = vol_volume_lookup_ref(volumes, INST_VOL_LABEL, &volume);
667 if (rc != EOK) {
668 sysinst_error(sysinst,
669 "Error creating volume server configuration.");
670 rc = EIO;
671 goto error;
672 }
673
674 sysinst_debug(sysinst, "Configuring volume server: set mount point");
675 rc = vol_volume_set_mountp(volume, INST_VOL_MP);
676 if (rc != EOK) {
677 sysinst_error(sysinst,
678 "Error creating system partition configuration.");
679 rc = EIO;
680 goto error;
681 }
682
683 rc = vol_volumes_sync(volumes);
684 if (rc != EOK) {
685 sysinst_error(sysinst, "Error saving volume confiuration.");
686 goto error;
687 }
688
689 log_msg(LOG_DEFAULT, LVL_NOTE,
690 "Configuring volume server: delete reference");
691 vol_volume_del_ref(volume);
692 volume = NULL;
693 log_msg(LOG_DEFAULT, LVL_NOTE,
694 "Configuring volume server: destroy volumes object");
695 vol_volumes_destroy(volumes);
696 volumes = NULL;
697
698 rc = rd_img_close(rd);
699 if (rc != EOK) {
700 sysinst_error(sysinst, "Error closing initial RAM disk image.");
701 rc = EIO;
702 goto error;
703 }
704
705 free(rdpath);
706 rdpath = NULL;
707 free(path);
708 path = NULL;
709
710 return EOK;
711error:
712 if (volume != NULL)
713 vol_volume_del_ref(volume);
714 if (volumes != NULL)
715 vol_volumes_destroy(volumes);
716 if (rd != NULL)
717 (void) rd_img_close(rd);
718 if (path != NULL)
719 free(path);
720 if (rdpath != NULL)
721 free(rdpath);
722 return rc;
723}
724
725/** Write unaligned 64-bit little-endian number.
726 *
727 * @param a Destination buffer
728 * @param data Number
729 */
730static void set_unaligned_u64le(uint8_t *a, uint64_t data)
731{
732 int i;
733
734 for (i = 0; i < 8; i++) {
735 a[i] = (data >> (i * 8)) & 0xff;
736 }
737}
738
739/** Copy boot blocks.
740 *
741 * Install Grub's boot blocks.
742 *
743 * @param sysinst System installer
744 * @param devp Disk device
745 * @return EOK on success or an error code
746 */
747static errno_t sysinst_copy_boot_blocks(sysinst_t *sysinst, const char *devp)
748{
749 void *boot_img;
750 size_t boot_img_size;
751 void *core_img;
752 size_t core_img_size;
753 service_id_t sid;
754 size_t bsize;
755 uint8_t bbuf[512];
756 aoff64_t core_start;
757 aoff64_t core_blocks;
758 grub_boot_blocklist_t *first_bl, *bl;
759 errno_t rc;
760
761 log_msg(LOG_DEFAULT, LVL_NOTE,
762 "sysinst_copy_boot_blocks: Read boot block image.");
763
764 rc = futil_get_file(sysinst->futil,
765 BOOT_FILES_SRC "/boot/grub/i386-pc/boot.img",
766 &boot_img, &boot_img_size);
767 if (rc != EOK || boot_img_size != 512)
768 return EIO;
769
770 log_msg(LOG_DEFAULT, LVL_NOTE,
771 "sysinst_copy_boot_blocks: Read GRUB core image.");
772
773 rc = futil_get_file(sysinst->futil,
774 BOOT_FILES_SRC "/boot/grub/i386-pc/core.img",
775 &core_img, &core_img_size);
776 if (rc != EOK)
777 return EIO;
778
779 log_msg(LOG_DEFAULT, LVL_NOTE,
780 "sysinst_copy_boot_blocks: get service ID.");
781
782 rc = loc_service_get_id(devp, &sid, 0);
783 if (rc != EOK)
784 return rc;
785
786 log_msg(LOG_DEFAULT, LVL_NOTE,
787 "sysinst_copy_boot_blocks: block_init.");
788
789 rc = block_init(sid);
790 if (rc != EOK)
791 return rc;
792
793 log_msg(LOG_DEFAULT, LVL_NOTE,
794 "sysinst_copy_boot_blocks: get block size");
795
796 rc = block_get_bsize(sid, &bsize);
797 if (rc != EOK)
798 return rc;
799
800 if (bsize != 512) {
801 sysinst_error(sysinst, "Device block size != 512.");
802 return EIO;
803 }
804
805 log_msg(LOG_DEFAULT, LVL_NOTE,
806 "sysinst_copy_boot_blocks: read boot block");
807
808 rc = block_read_direct(sid, BOOT_BLOCK_IDX, 1, bbuf);
809 if (rc != EOK)
810 return EIO;
811
812 core_start = 16;
813 core_blocks = (core_img_size + 511) / 512;
814
815 /* Clean blocklists */
816 first_bl = core_img + 512 - sizeof(*first_bl);
817 bl = first_bl;
818 while (bl->len != 0) {
819 memset(bl, 0, sizeof(*bl));
820 --bl;
821 if ((void *)bl < core_img) {
822 sysinst_error(sysinst,
823 "No block terminator in core image.");
824 return EIO;
825 }
826 }
827
828 first_bl->start = host2uint64_t_le(core_start + 1);
829 first_bl->len = host2uint16_t_le(core_blocks - 1);
830 first_bl->segment = grub_boot_i386_pc_kernel_seg + (512 >> 4);
831
832 /* Write boot code into boot block */
833 memcpy(bbuf, boot_img, 440); /* XXX 440 = sizeof(br_block_t.code_area) */
834 bbuf[grub_boot_machine_boot_drive] = 0xff;
835 set_unaligned_u64le(bbuf + grub_boot_machine_kernel_sector, core_start);
836
837 log_msg(LOG_DEFAULT, LVL_NOTE,
838 "sysinst_copy_boot_blocks: write boot block");
839
840 rc = block_write_direct(sid, BOOT_BLOCK_IDX, 1, bbuf);
841 if (rc != EOK)
842 return EIO;
843
844 log_msg(LOG_DEFAULT, LVL_NOTE,
845 "sysinst_copy_boot_blocks: write core blocks");
846
847 /* XXX Must pad last block with zeros */
848 rc = block_write_direct(sid, core_start, core_blocks, core_img);
849 if (rc != EOK)
850 return EIO;
851
852 log_msg(LOG_DEFAULT, LVL_NOTE,
853 "sysinst_copy_boot_blocks: OK.");
854
855 return EOK;
856}
857
858/** Eject installation volume.
859 *
860 * @param sysinst System installer
861 * @param part_id Partition service ID
862 * @return EOK on success or an error code
863 */
864static errno_t sysinst_eject_dev(sysinst_t *sysinst, service_id_t part_id)
865{
866 vol_t *vol = NULL;
867 errno_t rc;
868
869 rc = vol_create(&vol);
870 if (rc != EOK) {
871 sysinst_error(sysinst, "Error contacting volume service.");
872 goto out;
873 }
874
875 rc = vol_part_eject(vol, part_id, vef_none);
876 if (rc != EOK) {
877 sysinst_error(sysinst, "Error ejecting volume.");
878 goto out;
879 }
880
881 rc = EOK;
882out:
883 vol_destroy(vol);
884 return rc;
885}
886
887/** Physically eject volume by mount point.
888 *
889 * @param sysinst System installer
890 * @param path Mount point
891 * @return EOK on success or an error code
892 */
893static errno_t sysinst_eject_phys_by_mp(sysinst_t *sysinst, const char *path)
894{
895 vol_t *vol = NULL;
896 sysarg_t part_id;
897 errno_t rc;
898
899 rc = vol_create(&vol);
900 if (rc != EOK) {
901 sysinst_error(sysinst, "Error contacting volume service.");
902 goto out;
903 }
904
905 log_msg(LOG_DEFAULT, LVL_NOTE, "vol_part_by_mp: mp='%s'\n",
906 path);
907 rc = vol_part_by_mp(vol, path, &part_id);
908 if (rc != EOK) {
909 sysinst_error(sysinst,
910 "Error finding installation media mount point.");
911 goto out;
912 }
913
914 log_msg(LOG_DEFAULT, LVL_NOTE, "eject svc_id %lu", (unsigned long)part_id);
915 rc = vol_part_eject(vol, part_id, vef_physical);
916 if (rc != EOK) {
917 sysinst_error(sysinst, "Error ejecting volume.");
918 goto out;
919 }
920
921 rc = EOK;
922out:
923 vol_destroy(vol);
924 return rc;
925}
926
927/** Restart the system.
928 *
929 * @param sysinst System installer
930 * @return EOK on success or an error code
931 */
932static errno_t sysinst_restart(sysinst_t *sysinst)
933{
934 errno_t rc;
935 system_t *system;
936
937 fibril_mutex_initialize(&shutdown_lock);
938 fibril_condvar_initialize(&shutdown_cv);
939 shutdown_stopped = false;
940 shutdown_failed = false;
941
942 rc = system_open(SYSTEM_DEFAULT, &sysinst_system_cb, NULL, &system);
943 if (rc != EOK) {
944 sysinst_error(sysinst,
945 "Failed opening system control service.");
946 return rc;
947 }
948
949 rc = system_restart(system);
950 if (rc != EOK) {
951 system_close(system);
952 sysinst_error(sysinst, "Failed requesting system restart.");
953 return rc;
954 }
955
956 fibril_mutex_lock(&shutdown_lock);
957 sysinst_debug(sysinst, "The system is shutting down...");
958
959 while (!shutdown_stopped)
960 fibril_condvar_wait(&shutdown_cv, &shutdown_lock);
961
962 if (shutdown_failed) {
963 sysinst_error(sysinst, "Shutdown failed.");
964 system_close(system);
965 return rc;
966 }
967
968 log_msg(LOG_DEFAULT, LVL_NOTE,
969 "Shutdown complete. It is now safe to remove power.");
970
971 /* Sleep forever */
972 while (true)
973 fibril_condvar_wait(&shutdown_cv, &shutdown_lock);
974
975 fibril_mutex_unlock(&shutdown_lock);
976
977 system_close(system);
978 return 0;
979
980}
981
982/** Install system to a device.
983 *
984 * @parma sysinst System installer
985 * @param dev Device to install to.
986 * @return EOK on success or an error code
987 */
988static errno_t sysinst_install(sysinst_t *sysinst, const char *dev)
989{
990 errno_t rc;
991 bool clean_dev = false;
992
993 sysinst_action(sysinst, "Creating device label and file system.");
994
995 rc = sysinst_label_dev(sysinst, dev);
996 if (rc != EOK)
997 goto error;
998
999 clean_dev = true;
1000
1001 sysinst_action(sysinst, "Creating system directory structure.");
1002 rc = sysinst_setup_sysvol(sysinst);
1003 if (rc != EOK)
1004 goto error;
1005
1006 sysinst_action(sysinst, "Copying boot files.");
1007 rc = sysinst_copy_boot_files(sysinst);
1008 if (rc != EOK)
1009 goto error;
1010
1011 sysinst_action(sysinst, "Configuring the system.");
1012 rc = sysinst_customize_initrd(sysinst);
1013 if (rc != EOK)
1014 goto error;
1015
1016 sysinst_action(sysinst, "Finishing system volume.");
1017 rc = sysinst_finish_dev(sysinst);
1018 if (rc != EOK)
1019 goto error;
1020
1021 clean_dev = false;
1022
1023 sysinst_action(sysinst, "Installing boot blocks.");
1024 rc = sysinst_copy_boot_blocks(sysinst, dev);
1025 if (rc != EOK)
1026 return rc;
1027
1028 sysinst_action(sysinst, "Ejecting installation media.");
1029 rc = sysinst_eject_phys_by_mp(sysinst, CD_MOUNT_POINT);
1030 if (rc != EOK)
1031 return rc;
1032
1033 return EOK;
1034error:
1035 if (clean_dev)
1036 (void)sysinst_finish_dev(sysinst);
1037 return rc;
1038}
1039
1040/** Installation fibril.
1041 *
1042 * @param arg Argument (sysinst_t *)
1043 * @return EOK on success or an error code
1044 */
1045static errno_t sysinst_install_fibril(void *arg)
1046{
1047 sysinst_t *sysinst = (sysinst_t *)arg;
1048 unsigned i;
1049 errno_t rc;
1050
1051 (void)sysinst;
1052
1053 i = 0;
1054 while (default_devs[i] != NULL) {
1055 rc = sysinst_check_dev(default_devs[i]);
1056 if (rc == EOK)
1057 break;
1058 ++i;
1059 }
1060
1061 if (default_devs[i] == NULL) {
1062 sysinst_error(sysinst, "Cannot determine installation device.");
1063 rc = ENOENT;
1064 goto error;
1065 }
1066
1067 rc = sysinst_install(sysinst, default_devs[i]);
1068 if (rc != EOK)
1069 goto error;
1070
1071 sysinst_progress_destroy(sysinst->progress);
1072 rc = sysinst_restart_dlg_create(sysinst);
1073 if (rc != EOK)
1074 goto error;
1075
1076 return EOK;
1077error:
1078 ui_lock(sysinst->ui);
1079 sysinst_progress_destroy(sysinst->progress);
1080 (void)sysinst_error_msg_create(sysinst);
1081 ui_unlock(sysinst->ui);
1082 return rc;
1083}
1084
1085/** Create installation progress window.
1086 *
1087 * @param sysinst System installer
1088 * @param rprogress Place to store pointer to new progress window
1089 * @return EOK on success or an error code
1090 */
1091static errno_t sysinst_progress_create(sysinst_t *sysinst,
1092 sysinst_progress_t **rprogress)
1093{
1094 ui_wnd_params_t params;
1095 ui_window_t *window = NULL;
1096 gfx_rect_t rect;
1097 gfx_rect_t arect;
1098 ui_resource_t *ui_res;
1099 sysinst_progress_t *progress;
1100 ui_fixed_t *fixed = NULL;
1101 errno_t rc;
1102
1103 ui_wnd_params_init(&params);
1104 params.caption = "System Installation";
1105 params.style &= ~ui_wds_titlebar;
1106 params.flags |= ui_wndf_topmost;
1107 params.placement = ui_wnd_place_center;
1108 if (ui_is_textmode(sysinst->ui)) {
1109 params.rect.p0.x = 0;
1110 params.rect.p0.y = 0;
1111 params.rect.p1.x = 64;
1112 params.rect.p1.y = 5;
1113 } else {
1114 params.rect.p0.x = 0;
1115 params.rect.p0.y = 0;
1116 params.rect.p1.x = 500;
1117 params.rect.p1.y = 60;
1118 }
1119
1120 progress = calloc(1, sizeof(sysinst_progress_t));
1121 if (progress == NULL) {
1122 rc = ENOMEM;
1123 sysinst_error(sysinst, "Out of memory.");
1124 goto error;
1125 }
1126
1127 rc = ui_window_create(sysinst->ui, &params, &window);
1128 if (rc != EOK) {
1129 sysinst_error(sysinst, "Error creating window.");
1130 goto error;
1131 }
1132
1133 ui_window_set_cb(window, &progress_window_cb, (void *)sysinst);
1134
1135 ui_res = ui_window_get_res(window);
1136
1137 rc = ui_fixed_create(&fixed);
1138 if (rc != EOK) {
1139 sysinst_error(sysinst, "Error creating fixed layout.");
1140 goto error;
1141 }
1142
1143 rc = ui_label_create(ui_res, "Installing system. Please wait...",
1144 &progress->label);
1145 if (rc != EOK) {
1146 sysinst_error(sysinst, "Error creating label.");
1147 goto error;
1148 }
1149
1150 ui_window_get_app_rect(window, &arect);
1151
1152 if (ui_is_textmode(sysinst->ui)) {
1153 rect.p0.x = arect.p0.x;
1154 rect.p0.y = arect.p0.y;
1155 rect.p1.x = arect.p1.x;
1156 rect.p1.y = 2;
1157 } else {
1158 rect.p0.x = arect.p0.x;
1159 rect.p0.y = arect.p0.y;
1160 rect.p1.x = arect.p1.x;
1161 rect.p1.y = 30;
1162 }
1163 ui_label_set_rect(progress->label, &rect);
1164 ui_label_set_halign(progress->label, gfx_halign_center);
1165 ui_label_set_valign(progress->label, gfx_valign_center);
1166
1167 rc = ui_fixed_add(fixed, ui_label_ctl(progress->label));
1168 if (rc != EOK) {
1169 sysinst_error(sysinst, "Error adding control to layout.");
1170 ui_label_destroy(progress->label);
1171 progress->label = NULL;
1172 goto error;
1173 }
1174
1175 rc = ui_label_create(ui_res, "",
1176 &progress->action);
1177 if (rc != EOK) {
1178 sysinst_error(sysinst, "Error creating label.");
1179 goto error;
1180 }
1181
1182 if (ui_is_textmode(sysinst->ui)) {
1183 rect.p0.x = arect.p0.x;
1184 rect.p0.y = 3;
1185 rect.p1.x = arect.p1.x;
1186 rect.p1.y = arect.p1.y;
1187 } else {
1188 rect.p0.x = arect.p0.x;
1189 rect.p0.y = 30;
1190 rect.p1.x = arect.p1.x;
1191 rect.p1.y = arect.p1.y;
1192 }
1193 ui_label_set_rect(progress->action, &rect);
1194 ui_label_set_halign(progress->action, gfx_halign_center);
1195 ui_label_set_valign(progress->action, gfx_valign_center);
1196
1197 rc = ui_fixed_add(fixed, ui_label_ctl(progress->action));
1198 if (rc != EOK) {
1199 sysinst_error(sysinst, "Error adding control to layout.");
1200 ui_label_destroy(progress->label);
1201 progress->label = NULL;
1202 goto error;
1203 }
1204
1205 ui_window_add(window, ui_fixed_ctl(fixed));
1206 fixed = NULL;
1207
1208 rc = ui_window_paint(window);
1209 if (rc != EOK) {
1210 sysinst_error(sysinst, "Error painting window.");
1211 goto error;
1212 }
1213
1214 progress->window = window;
1215 progress->fixed = fixed;
1216 *rprogress = progress;
1217 return EOK;
1218error:
1219 if (progress != NULL && progress->fixed != NULL)
1220 ui_fixed_destroy(progress->fixed);
1221 if (window != NULL)
1222 ui_window_destroy(window);
1223 if (progress != NULL)
1224 free(progress);
1225 return rc;
1226}
1227
1228/** Destroy installation progress window.
1229 *
1230 * @param sysinst System installer
1231 * @param rprogress Place to store pointer to new progress window
1232 * @return EOK on success or an error code
1233 */
1234static void sysinst_progress_destroy(sysinst_progress_t *progress)
1235{
1236 if (progress == NULL)
1237 return;
1238
1239 ui_window_destroy(progress->window);
1240 free(progress);
1241}
1242
1243/** Set current action message.
1244 *
1245 * @param sysinst System installer
1246 * @param action Action text
1247 */
1248static void sysinst_action(sysinst_t *sysinst, const char *action)
1249{
1250 if (sysinst->progress == NULL)
1251 return;
1252
1253 ui_label_set_text(sysinst->progress->action, action);
1254 ui_label_paint(sysinst->progress->action);
1255 log_msg(LOG_DEFAULT, LVL_NOTE, "%s", action);
1256}
1257
1258/** Set current error message.
1259 *
1260 * @param sysinst System installer
1261 * @param errmsg Error message
1262 */
1263static void sysinst_error(sysinst_t *sysinst, const char *errmsg)
1264{
1265 str_cpy(sysinst->errmsg, sizeof(sysinst->errmsg), errmsg);
1266 log_msg(LOG_DEFAULT, LVL_ERROR, errmsg);
1267}
1268
1269/** Log a debug message.
1270 *
1271 * @param sysinst System installer
1272 * @param errmsg Error message
1273 */
1274static void sysinst_debug(sysinst_t *sysinst, const char *msg)
1275{
1276 log_msg(LOG_DEFAULT, LVL_ERROR, msg);
1277}
1278
1279/** Start system installation.
1280 *
1281 * @param sysinst System installer
1282 * @return EOK on success or an error code
1283 */
1284static int sysinst_start(sysinst_t *sysinst)
1285{
1286 errno_t rc;
1287 fid_t fid;
1288
1289 rc = sysinst_progress_create(sysinst, &sysinst->progress);
1290 if (rc != EOK)
1291 return rc;
1292
1293 fid = fibril_create(sysinst_install_fibril, (void *)sysinst);
1294 if (fid == 0) {
1295 sysinst_error(sysinst, "Out of memory.");
1296 return ENOMEM;
1297 }
1298
1299 fibril_add_ready(fid);
1300 return EOK;
1301}
1302
1303/** Create installation confirmation dialog.
1304 *
1305 * @param sysinst System installer
1306 * @return EOK on success or an error code
1307 */
1308static errno_t sysinst_confirm_create(sysinst_t *sysinst)
1309{
1310 ui_msg_dialog_params_t params;
1311 ui_msg_dialog_t *dialog;
1312 errno_t rc;
1313
1314 ui_msg_dialog_params_init(&params);
1315 params.caption = "System installation";
1316 params.text = "This will install HelenOS to your computer. Continue?";
1317 params.choice = umdc_ok_cancel;
1318 params.flags |= umdf_topmost | umdf_center;
1319
1320 rc = ui_msg_dialog_create(sysinst->ui, &params, &dialog);
1321 if (rc != EOK)
1322 return rc;
1323
1324 ui_msg_dialog_set_cb(dialog, &sysinst_confirm_cb, sysinst);
1325 return EOK;
1326}
1327
1328/** Create restart dialog.
1329 *
1330 * @param sysinst System installer
1331 * @return EOK on success or an error code
1332 */
1333static errno_t sysinst_restart_dlg_create(sysinst_t *sysinst)
1334{
1335 ui_msg_dialog_params_t params;
1336 ui_msg_dialog_t *dialog;
1337 errno_t rc;
1338
1339 ui_msg_dialog_params_init(&params);
1340 params.caption = "Restart System";
1341 params.text = "Installation complete. Restart the system?";
1342 params.choice = umdc_ok_cancel;
1343 params.flags |= umdf_topmost | umdf_center;
1344
1345 rc = ui_msg_dialog_create(sysinst->ui, &params, &dialog);
1346 if (rc != EOK)
1347 return rc;
1348
1349 ui_msg_dialog_set_cb(dialog, &sysinst_restart_dlg_cb, sysinst);
1350 return EOK;
1351}
1352
1353/** Run system installer on display.
1354 *
1355 * @param display_spec Display specification
1356 * @return EOK on success or an error code
1357 */
1358static errno_t sysinst_run(const char *display_spec)
1359{
1360 ui_t *ui = NULL;
1361 sysinst_t *sysinst;
1362 ui_wnd_params_t params;
1363 errno_t rc;
1364
1365 sysinst = calloc(1, sizeof(sysinst_t));
1366 if (sysinst == NULL)
1367 return ENOMEM;
1368
1369 rc = futil_create(&sysinst_futil_cb, (void *)sysinst, &sysinst->futil);
1370 if (rc != EOK) {
1371 printf("Out of memory.\n");
1372 goto error;
1373 }
1374
1375 rc = ui_create(display_spec, &ui);
1376 if (rc != EOK) {
1377 printf("Error creating UI on display %s.\n", display_spec);
1378 goto error;
1379 }
1380
1381 sysinst->ui = ui;
1382
1383 ui_wnd_params_init(&params);
1384 params.caption = "System Installation";
1385 params.style &= ~ui_wds_decorated;
1386 params.placement = ui_wnd_place_full_screen;
1387 params.flags |= ui_wndf_topmost | ui_wndf_nofocus;
1388
1389 rc = ui_window_create(sysinst->ui, &params, &sysinst->bgwindow);
1390 if (rc != EOK) {
1391 printf("Error creating window.\n");
1392 goto error;
1393 }
1394
1395 ui_window_set_cb(sysinst->bgwindow, &bg_window_cb, (void *)sysinst);
1396
1397 if (ui_is_textmode(sysinst->ui)) {
1398 rc = gfx_color_new_ega(0x17, &sysinst->bg_color);
1399 if (rc != EOK) {
1400 printf("Error allocating color.\n");
1401 goto error;
1402 }
1403 } else {
1404 rc = gfx_color_new_rgb_i16(0x8000, 0xc800, 0xffff, &sysinst->bg_color);
1405 if (rc != EOK) {
1406 printf("Error allocating color.\n");
1407 goto error;
1408 }
1409 }
1410
1411 rc = ui_window_paint(sysinst->bgwindow);
1412 if (rc != EOK) {
1413 printf("Error painting window.\n");
1414 goto error;
1415 }
1416
1417 (void)sysinst_confirm_create(sysinst);
1418
1419 ui_run(ui);
1420
1421 if (sysinst->bgwindow != NULL)
1422 ui_window_destroy(sysinst->bgwindow);
1423 if (sysinst->system != NULL)
1424 system_close(sysinst->system);
1425 gfx_color_delete(sysinst->bg_color);
1426 ui_destroy(ui);
1427 free(sysinst);
1428 return EOK;
1429error:
1430 if (sysinst->futil != NULL)
1431 futil_destroy(sysinst->futil);
1432 if (sysinst->system != NULL)
1433 system_close(sysinst->system);
1434 if (sysinst->bg_color != NULL)
1435 gfx_color_delete(sysinst->bg_color);
1436 if (sysinst->bgwindow != NULL)
1437 ui_window_destroy(sysinst->bgwindow);
1438 if (ui != NULL)
1439 ui_destroy(ui);
1440 free(sysinst);
1441 return rc;
1442}
1443
1444static void print_syntax(void)
1445{
1446 printf("Syntax: " NAME " [-d <display-spec>]\n");
1447}
1448
1449int main(int argc, char *argv[])
1450{
1451 const char *display_spec = UI_ANY_DEFAULT;
1452 errno_t rc;
1453 int i;
1454
1455 i = 1;
1456 while (i < argc && argv[i][0] == '-') {
1457 if (str_cmp(argv[i], "-d") == 0) {
1458 ++i;
1459 if (i >= argc) {
1460 printf("Argument missing.\n");
1461 print_syntax();
1462 return 1;
1463 }
1464
1465 display_spec = argv[i++];
1466 } else {
1467 printf("Invalid option '%s'.\n", argv[i]);
1468 print_syntax();
1469 return 1;
1470 }
1471 }
1472
1473 if (i < argc) {
1474 print_syntax();
1475 return 1;
1476 }
1477
1478 if (log_init(NAME) != EOK) {
1479 printf(NAME ": Failed to initialize logging.\n");
1480 return 1;
1481 }
1482
1483 rc = sysinst_run(display_spec);
1484 if (rc != EOK)
1485 return 1;
1486
1487 return 0;
1488}
1489
1490/** @}
1491 */
Note: See TracBrowser for help on using the repository browser.