| 1 | /*
|
|---|
| 2 | * Copyright (c) 2018 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 <cap.h>
|
|---|
| 41 | #include <errno.h>
|
|---|
| 42 | #include <fdisk.h>
|
|---|
| 43 | #include <loc.h>
|
|---|
| 44 | #include <stdio.h>
|
|---|
| 45 | #include <stdlib.h>
|
|---|
| 46 | #include <str.h>
|
|---|
| 47 | #include <str_error.h>
|
|---|
| 48 | #include <vfs/vfs.h>
|
|---|
| 49 | #include <vol.h>
|
|---|
| 50 |
|
|---|
| 51 | #include "futil.h"
|
|---|
| 52 | #include "grub.h"
|
|---|
| 53 |
|
|---|
| 54 | /** Device to install to
|
|---|
| 55 | *
|
|---|
| 56 | * Note that you cannot simply change this, because the installation
|
|---|
| 57 | * device is hardcoded in core.img. If you wanted to install to another
|
|---|
| 58 | * device, you must build your own core.img (e.g. using tools/grub/mkimage.sh
|
|---|
| 59 | * and modifying tools/grub/load.cfg, supplying the device to boot from
|
|---|
| 60 | * in Grub notation).
|
|---|
| 61 | */
|
|---|
| 62 | #define DEFAULT_DEV "devices/\\hw\\pci0\\00:01.0\\ata-c1\\d0"
|
|---|
| 63 | //#define DEFAULT_DEV "devices/\\hw\\pci0\\00:01.2\\uhci_rh\\usb01_a1\\mass-storage0\\l0"
|
|---|
| 64 | /** Volume label for the new file system */
|
|---|
| 65 | #define INST_VOL_LABEL "HelenOS"
|
|---|
| 66 |
|
|---|
| 67 | #define MOUNT_POINT "/inst"
|
|---|
| 68 |
|
|---|
| 69 | /** HelenOS live CD volume label */
|
|---|
| 70 | #define CD_VOL_LABEL "HelenOS-CD"
|
|---|
| 71 | /** XXX Should get this from the volume server */
|
|---|
| 72 | #define CD_MOUNT_POINT "/vol/" CD_VOL_LABEL
|
|---|
| 73 |
|
|---|
| 74 | #define BOOT_FILES_SRC CD_MOUNT_POINT
|
|---|
| 75 | #define BOOT_BLOCK_IDX 0 /* MBR */
|
|---|
| 76 |
|
|---|
| 77 | /** Label the destination device.
|
|---|
| 78 | *
|
|---|
| 79 | * @param dev Disk device to label
|
|---|
| 80 | * @param psvc_id Place to store service ID of the created partition
|
|---|
| 81 | *
|
|---|
| 82 | * @return EOK on success or an error code
|
|---|
| 83 | */
|
|---|
| 84 | static errno_t sysinst_label_dev(const char *dev, service_id_t *psvc_id)
|
|---|
| 85 | {
|
|---|
| 86 | fdisk_t *fdisk;
|
|---|
| 87 | fdisk_dev_t *fdev;
|
|---|
| 88 | fdisk_part_t *part;
|
|---|
| 89 | fdisk_part_spec_t pspec;
|
|---|
| 90 | fdisk_part_info_t pinfo;
|
|---|
| 91 | cap_spec_t cap;
|
|---|
| 92 | service_id_t sid;
|
|---|
| 93 | errno_t rc;
|
|---|
| 94 |
|
|---|
| 95 | printf("sysinst_label_dev(): get service ID '%s'\n", dev);
|
|---|
| 96 | rc = loc_service_get_id(dev, &sid, 0);
|
|---|
| 97 | if (rc != EOK)
|
|---|
| 98 | return rc;
|
|---|
| 99 |
|
|---|
| 100 | printf("sysinst_label_dev(): open device\n");
|
|---|
| 101 |
|
|---|
| 102 | rc = fdisk_create(&fdisk);
|
|---|
| 103 | if (rc != EOK) {
|
|---|
| 104 | printf("Error initializing fdisk.\n");
|
|---|
| 105 | return rc;
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | rc = fdisk_dev_open(fdisk, sid, &fdev);
|
|---|
| 109 | if (rc != EOK) {
|
|---|
| 110 | printf("Error opening device.\n");
|
|---|
| 111 | return rc;
|
|---|
| 112 | }
|
|---|
| 113 |
|
|---|
| 114 | printf("sysinst_label_dev(): create mount directory\n");
|
|---|
| 115 |
|
|---|
| 116 | rc = vfs_link_path(MOUNT_POINT, KIND_DIRECTORY, NULL);
|
|---|
| 117 | if (rc != EOK)
|
|---|
| 118 | return rc;
|
|---|
| 119 |
|
|---|
| 120 | printf("sysinst_label_dev(): create label\n");
|
|---|
| 121 |
|
|---|
| 122 | rc = fdisk_label_create(fdev, lt_mbr);
|
|---|
| 123 | if (rc != EOK) {
|
|---|
| 124 | printf("Error creating label: %s.\n", str_error(rc));
|
|---|
| 125 | return rc;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | printf("sysinst_label_dev(): create partition\n");
|
|---|
| 129 |
|
|---|
| 130 | rc = fdisk_part_get_max_avail(fdev, spc_pri, &cap);
|
|---|
| 131 | if (rc != EOK) {
|
|---|
| 132 | printf("Error getting available capacity: %s.\n", str_error(rc));
|
|---|
| 133 | return rc;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | fdisk_pspec_init(&pspec);
|
|---|
| 137 | pspec.capacity = cap;
|
|---|
| 138 | pspec.pkind = lpk_primary;
|
|---|
| 139 | pspec.fstype = fs_ext4; /* Cannot be changed without modifying core.img */
|
|---|
| 140 | pspec.mountp = MOUNT_POINT;
|
|---|
| 141 | pspec.label = INST_VOL_LABEL;
|
|---|
| 142 |
|
|---|
| 143 | rc = fdisk_part_create(fdev, &pspec, &part);
|
|---|
| 144 | if (rc != EOK) {
|
|---|
| 145 | printf("Error creating partition.\n");
|
|---|
| 146 | return rc;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | rc = fdisk_part_get_info(part, &pinfo);
|
|---|
| 150 | if (rc != EOK) {
|
|---|
| 151 | printf("Error getting partition information.\n");
|
|---|
| 152 | return rc;
|
|---|
| 153 | }
|
|---|
| 154 |
|
|---|
| 155 | printf("sysinst_label_dev(): OK\n");
|
|---|
| 156 | *psvc_id = pinfo.svc_id;
|
|---|
| 157 | return EOK;
|
|---|
| 158 | }
|
|---|
| 159 |
|
|---|
| 160 | /** Copy boot files.
|
|---|
| 161 | *
|
|---|
| 162 | * @return EOK on success or an error code
|
|---|
| 163 | */
|
|---|
| 164 | static errno_t sysinst_copy_boot_files(void)
|
|---|
| 165 | {
|
|---|
| 166 | errno_t rc;
|
|---|
| 167 |
|
|---|
| 168 | printf("sysinst_copy_boot_files(): copy bootloader files\n");
|
|---|
| 169 | rc = futil_rcopy_contents(BOOT_FILES_SRC, MOUNT_POINT);
|
|---|
| 170 | if (rc != EOK)
|
|---|
| 171 | return rc;
|
|---|
| 172 |
|
|---|
| 173 | printf("sysinst_copy_boot_files(): OK\n");
|
|---|
| 174 | return EOK;
|
|---|
| 175 | }
|
|---|
| 176 |
|
|---|
| 177 | /** Write unaligned 64-bit little-endian number.
|
|---|
| 178 | *
|
|---|
| 179 | * @param a Destination buffer
|
|---|
| 180 | * @param data Number
|
|---|
| 181 | */
|
|---|
| 182 | static void set_unaligned_u64le(uint8_t *a, uint64_t data)
|
|---|
| 183 | {
|
|---|
| 184 | int i;
|
|---|
| 185 |
|
|---|
| 186 | for (i = 0; i < 8; i++) {
|
|---|
| 187 | a[i] = (data >> (i * 8)) & 0xff;
|
|---|
| 188 | }
|
|---|
| 189 | }
|
|---|
| 190 |
|
|---|
| 191 | /** Copy boot blocks.
|
|---|
| 192 | *
|
|---|
| 193 | * Install Grub's boot blocks.
|
|---|
| 194 | *
|
|---|
| 195 | * @param devp Disk device
|
|---|
| 196 | * @return EOK on success or an error code
|
|---|
| 197 | */
|
|---|
| 198 | static errno_t sysinst_copy_boot_blocks(const char *devp)
|
|---|
| 199 | {
|
|---|
| 200 | void *boot_img;
|
|---|
| 201 | size_t boot_img_size;
|
|---|
| 202 | void *core_img;
|
|---|
| 203 | size_t core_img_size;
|
|---|
| 204 | service_id_t sid;
|
|---|
| 205 | size_t bsize;
|
|---|
| 206 | uint8_t bbuf[512];
|
|---|
| 207 | aoff64_t core_start;
|
|---|
| 208 | aoff64_t core_blocks;
|
|---|
| 209 | grub_boot_blocklist_t *first_bl, *bl;
|
|---|
| 210 | errno_t rc;
|
|---|
| 211 |
|
|---|
| 212 | printf("sysinst_copy_boot_blocks: Read boot block image.\n");
|
|---|
| 213 | rc = futil_get_file(BOOT_FILES_SRC "/boot/grub/i386-pc/boot.img",
|
|---|
| 214 | &boot_img, &boot_img_size);
|
|---|
| 215 | if (rc != EOK || boot_img_size != 512)
|
|---|
| 216 | return EIO;
|
|---|
| 217 |
|
|---|
| 218 | printf("sysinst_copy_boot_blocks: Read GRUB core image.\n");
|
|---|
| 219 | rc = futil_get_file(BOOT_FILES_SRC "/boot/grub/i386-pc/core.img",
|
|---|
| 220 | &core_img, &core_img_size);
|
|---|
| 221 | if (rc != EOK)
|
|---|
| 222 | return EIO;
|
|---|
| 223 |
|
|---|
| 224 | printf("sysinst_copy_boot_blocks: get service ID.\n");
|
|---|
| 225 | rc = loc_service_get_id(devp, &sid, 0);
|
|---|
| 226 | if (rc != EOK)
|
|---|
| 227 | return rc;
|
|---|
| 228 |
|
|---|
| 229 | printf("sysinst_copy_boot_blocks: block_init.\n");
|
|---|
| 230 | rc = block_init(sid, 512);
|
|---|
| 231 | if (rc != EOK)
|
|---|
| 232 | return rc;
|
|---|
| 233 |
|
|---|
| 234 | printf("sysinst_copy_boot_blocks: get block size\n");
|
|---|
| 235 | rc = block_get_bsize(sid, &bsize);
|
|---|
| 236 | if (rc != EOK)
|
|---|
| 237 | return rc;
|
|---|
| 238 |
|
|---|
| 239 | if (bsize != 512) {
|
|---|
| 240 | printf("Device block size != 512.\n");
|
|---|
| 241 | return EIO;
|
|---|
| 242 | }
|
|---|
| 243 |
|
|---|
| 244 | printf("sysinst_copy_boot_blocks: read boot block\n");
|
|---|
| 245 | rc = block_read_direct(sid, BOOT_BLOCK_IDX, 1, bbuf);
|
|---|
| 246 | if (rc != EOK)
|
|---|
| 247 | return EIO;
|
|---|
| 248 |
|
|---|
| 249 | core_start = 16;
|
|---|
| 250 | core_blocks = (core_img_size + 511) / 512;
|
|---|
| 251 |
|
|---|
| 252 | /* Clean blocklists */
|
|---|
| 253 | first_bl = core_img + 512 - sizeof(*first_bl);
|
|---|
| 254 | bl = first_bl;
|
|---|
| 255 | while (bl->len != 0) {
|
|---|
| 256 | memset(bl, 0, sizeof(*bl));
|
|---|
| 257 | --bl;
|
|---|
| 258 | if ((void *)bl < core_img) {
|
|---|
| 259 | printf("No block terminator in core image.\n");
|
|---|
| 260 | return EIO;
|
|---|
| 261 | }
|
|---|
| 262 | }
|
|---|
| 263 |
|
|---|
| 264 | first_bl->start = host2uint64_t_le(core_start + 1);
|
|---|
| 265 | first_bl->len = host2uint16_t_le(core_blocks - 1);
|
|---|
| 266 | first_bl->segment = grub_boot_i386_pc_kernel_seg + (512 >> 4);
|
|---|
| 267 |
|
|---|
| 268 | /* Write boot code into boot block */
|
|---|
| 269 | memcpy(bbuf, boot_img, 440); /* XXX 440 = sizeof(br_block_t.code_area) */
|
|---|
| 270 | bbuf[grub_boot_machine_boot_drive] = 0xff;
|
|---|
| 271 | set_unaligned_u64le(bbuf + grub_boot_machine_kernel_sector, core_start);
|
|---|
| 272 |
|
|---|
| 273 | printf("sysinst_copy_boot_blocks: write boot block\n");
|
|---|
| 274 | rc = block_write_direct(sid, BOOT_BLOCK_IDX, 1, bbuf);
|
|---|
| 275 | if (rc != EOK)
|
|---|
| 276 | return EIO;
|
|---|
| 277 |
|
|---|
| 278 | printf("sysinst_copy_boot_blocks: write core blocks\n");
|
|---|
| 279 | /* XXX Must pad last block with zeros */
|
|---|
| 280 | rc = block_write_direct(sid, core_start, core_blocks, core_img);
|
|---|
| 281 | if (rc != EOK)
|
|---|
| 282 | return EIO;
|
|---|
| 283 |
|
|---|
| 284 | printf("sysinst_copy_boot_blocks: OK.\n");
|
|---|
| 285 | return EOK;
|
|---|
| 286 | }
|
|---|
| 287 |
|
|---|
| 288 | /** Eject installation volume.
|
|---|
| 289 | *
|
|---|
| 290 | * @param psvc_id Partition service ID
|
|---|
| 291 | */
|
|---|
| 292 | static errno_t sysinst_eject_dev(service_id_t part_id)
|
|---|
| 293 | {
|
|---|
| 294 | vol_t *vol = NULL;
|
|---|
| 295 | errno_t rc;
|
|---|
| 296 |
|
|---|
| 297 | rc = vol_create(&vol);
|
|---|
| 298 | if (rc != EOK) {
|
|---|
| 299 | printf("Error contacting volume service.\n");
|
|---|
| 300 | goto out;
|
|---|
| 301 | }
|
|---|
| 302 |
|
|---|
| 303 | rc = vol_part_eject(vol, part_id);
|
|---|
| 304 | if (rc != EOK) {
|
|---|
| 305 | printf("Error ejecting volume.\n");
|
|---|
| 306 | goto out;
|
|---|
| 307 | }
|
|---|
| 308 |
|
|---|
| 309 | rc = EOK;
|
|---|
| 310 | out:
|
|---|
| 311 | vol_destroy(vol);
|
|---|
| 312 | return rc;
|
|---|
| 313 | }
|
|---|
| 314 |
|
|---|
| 315 | /** Install system to a device.
|
|---|
| 316 | *
|
|---|
| 317 | * @param dev Device to install to.
|
|---|
| 318 | * @return EOK on success or an error code
|
|---|
| 319 | */
|
|---|
| 320 | static errno_t sysinst_install(const char *dev)
|
|---|
| 321 | {
|
|---|
| 322 | errno_t rc;
|
|---|
| 323 | service_id_t psvc_id;
|
|---|
| 324 |
|
|---|
| 325 | rc = sysinst_label_dev(dev, &psvc_id);
|
|---|
| 326 | if (rc != EOK)
|
|---|
| 327 | return rc;
|
|---|
| 328 |
|
|---|
| 329 | printf("FS created and mounted. Copying boot files.\n");
|
|---|
| 330 | rc = sysinst_copy_boot_files();
|
|---|
| 331 | if (rc != EOK)
|
|---|
| 332 | return rc;
|
|---|
| 333 |
|
|---|
| 334 | printf("Boot files done. Installing boot blocks.\n");
|
|---|
| 335 | rc = sysinst_copy_boot_blocks(dev);
|
|---|
| 336 | if (rc != EOK)
|
|---|
| 337 | return rc;
|
|---|
| 338 |
|
|---|
| 339 | printf("Ejecting device.\n");
|
|---|
| 340 | rc = sysinst_eject_dev(psvc_id);
|
|---|
| 341 | if (rc != EOK)
|
|---|
| 342 | return rc;
|
|---|
| 343 |
|
|---|
| 344 | return EOK;
|
|---|
| 345 | }
|
|---|
| 346 |
|
|---|
| 347 | int main(int argc, char *argv[])
|
|---|
| 348 | {
|
|---|
| 349 | const char *dev = DEFAULT_DEV;
|
|---|
| 350 | return sysinst_install(dev);
|
|---|
| 351 | }
|
|---|
| 352 |
|
|---|
| 353 | /** @}
|
|---|
| 354 | */
|
|---|