source: mainline/uspace/app/sysinst/sysinst.c@ 5f36841

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 5f36841 was 5f36841, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Basic automatic volume mounting.

  • Property mode set to 100644
File size: 9.1 KB
Line 
1/*
2 * Copyright (c) 2014 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_error.h>
47#include <task.h>
48#include <vfs/vfs.h>
49#include <str.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
65/** Filysystem type. Cannot be changed without building a custom core.img */
66#define FS_TYPE "mfs"
67
68#define FS_SRV "/srv/mfs"
69#define MOUNT_POINT "/inst"
70
71/** HelenOS live CD volume label */
72#define CD_VOL_LABEL "HelenOS-CD"
73#define CD_MOUNT_POINT "/vol/" CD_VOL_LABEL
74
75#define BOOT_FILES_SRC CD_MOUNT_POINT
76#define BOOT_BLOCK_IDX 0 /* MBR */
77
78/** Label the destination device.
79 *
80 * @param dev Disk device to label
81 * @param pdev Place to store partition device name
82 *
83 * @return EOK on success or an error code
84 */
85static errno_t sysinst_label_dev(const char *dev, char **pdev)
86{
87 fdisk_t *fdisk;
88 fdisk_dev_t *fdev;
89 fdisk_part_t *part;
90 fdisk_part_spec_t pspec;
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 label\n");
115
116 rc = fdisk_label_create(fdev, lt_mbr);
117 if (rc != EOK) {
118 printf("Error creating label: %s.\n", str_error(rc));
119 return rc;
120 }
121
122 printf("sysinst_label_dev(): create partition\n");
123
124 rc = fdisk_part_get_max_avail(fdev, spc_pri, &cap);
125 if (rc != EOK) {
126 printf("Error getting available capacity: %s.\n", str_error(rc));
127 return rc;
128 }
129
130 fdisk_pspec_init(&pspec);
131 pspec.capacity = cap;
132 pspec.pkind = lpk_primary;
133 pspec.fstype = fs_minix;
134
135 rc = fdisk_part_create(fdev, &pspec, &part);
136 if (rc != EOK) {
137 printf("Error creating partition.\n");
138 return rc;
139 }
140
141 /* XXX libfdisk should give us the service name */
142 if (asprintf(pdev, "%sp1", dev) < 0)
143 return ENOMEM;
144
145 printf("sysinst_label_dev(): OK\n");
146 return EOK;
147}
148
149/** Mount target file system.
150 *
151 * @param dev Partition device
152 * @return EOK on success or an error code
153 */
154static errno_t sysinst_fs_mount(const char *dev)
155{
156 task_wait_t twait;
157 task_exit_t texit;
158 errno_t rc;
159 int trc;
160
161 printf("sysinst_fs_mount(): start filesystem server\n");
162 rc = task_spawnl(NULL, &twait, FS_SRV, FS_SRV, NULL);
163 if (rc != EOK)
164 return rc;
165
166 printf("sysinst_fs_mount(): wait for filesystem server\n");
167 rc = task_wait(&twait, &texit, &trc);
168 if (rc != EOK)
169 return rc;
170
171 printf("sysinst_fs_mount(): verify filesystem server result\n");
172 if (texit != TASK_EXIT_NORMAL || trc != 0) {
173 printf("sysinst_fs_mount(): not successful, but could be already loaded.\n");
174 }
175
176 rc = vfs_link_path(MOUNT_POINT, KIND_DIRECTORY, NULL);
177 if (rc != EOK)
178 return rc;
179
180 printf("sysinst_fs_mount(): mount filesystem\n");
181 rc = vfs_mount_path(MOUNT_POINT, FS_TYPE, dev, "", 0, 0);
182 if (rc != EOK)
183 return rc;
184
185 printf("sysinst_fs_mount(): OK\n");
186 return EOK;
187}
188
189/** Copy boot files.
190 *
191 * @return EOK on success or an error code
192 */
193static errno_t sysinst_copy_boot_files(void)
194{
195 errno_t rc;
196
197 printf("sysinst_copy_boot_files(): copy bootloader files\n");
198 rc = futil_rcopy_contents(BOOT_FILES_SRC, MOUNT_POINT);
199 if (rc != EOK)
200 return rc;
201
202 printf("sysinst_copy_boot_files(): unmount %s\n", MOUNT_POINT);
203 rc = vfs_unmount_path(MOUNT_POINT);
204 if (rc != EOK)
205 return rc;
206
207 printf("sysinst_copy_boot_files(): OK\n");
208 return EOK;
209}
210
211/** Write unaligned 64-bit little-endian number.
212 *
213 * @param a Destination buffer
214 * @param data Number
215 */
216static void set_unaligned_u64le(uint8_t *a, uint64_t data)
217{
218 int i;
219
220 for (i = 0; i < 8; i++) {
221 a[i] = (data >> (i * 8)) & 0xff;
222 }
223}
224
225/** Copy boot blocks.
226 *
227 * Install Grub's boot blocks.
228 *
229 * @param devp Disk device
230 * @return EOK on success or an error code
231 */
232static errno_t sysinst_copy_boot_blocks(const char *devp)
233{
234 void *boot_img;
235 size_t boot_img_size;
236 void *core_img;
237 size_t core_img_size;
238 service_id_t sid;
239 size_t bsize;
240 uint8_t bbuf[512];
241 aoff64_t core_start;
242 aoff64_t core_blocks;
243 grub_boot_blocklist_t *first_bl, *bl;
244 errno_t rc;
245
246 printf("sysinst_copy_boot_blocks: Read boot block image.\n");
247 rc = futil_get_file(BOOT_FILES_SRC "/boot/grub/i386-pc/boot.img",
248 &boot_img, &boot_img_size);
249 if (rc != EOK || boot_img_size != 512)
250 return EIO;
251
252 printf("sysinst_copy_boot_blocks: Read GRUB core image.\n");
253 rc = futil_get_file(BOOT_FILES_SRC "/boot/grub/i386-pc/core.img",
254 &core_img, &core_img_size);
255 if (rc != EOK)
256 return EIO;
257
258 printf("sysinst_copy_boot_blocks: get service ID.\n");
259 rc = loc_service_get_id(devp, &sid, 0);
260 if (rc != EOK)
261 return rc;
262
263 printf("sysinst_copy_boot_blocks: block_init.\n");
264 rc = block_init(sid, 512);
265 if (rc != EOK)
266 return rc;
267
268 printf("sysinst_copy_boot_blocks: get block size\n");
269 rc = block_get_bsize(sid, &bsize);
270 if (rc != EOK)
271 return rc;
272
273 if (bsize != 512) {
274 printf("Device block size != 512.\n");
275 return EIO;
276 }
277
278 printf("sysinst_copy_boot_blocks: read boot block\n");
279 rc = block_read_direct(sid, BOOT_BLOCK_IDX, 1, bbuf);
280 if (rc != EOK)
281 return EIO;
282
283 core_start = 16;
284 core_blocks = (core_img_size + 511) / 512;
285
286 /* Clean blocklists */
287 first_bl = core_img + 512 - sizeof(*first_bl);
288 bl = first_bl;
289 while (bl->len != 0) {
290 memset(bl, 0, sizeof(*bl));
291 --bl;
292 if ((void *)bl < core_img) {
293 printf("No block terminator in core image.\n");
294 return EIO;
295 }
296 }
297
298 first_bl->start = host2uint64_t_le(core_start + 1);
299 first_bl->len = host2uint16_t_le(core_blocks - 1);
300 first_bl->segment = grub_boot_i386_pc_kernel_seg + (512 >> 4);
301
302 /* Write boot code into boot block */
303 memcpy(bbuf, boot_img, 440); /* XXX 440 = sizeof(br_block_t.code_area) */
304 bbuf[grub_boot_machine_boot_drive] = 0xff;
305 set_unaligned_u64le(bbuf + grub_boot_machine_kernel_sector, core_start);
306
307 printf("sysinst_copy_boot_blocks: write boot block\n");
308 rc = block_write_direct(sid, BOOT_BLOCK_IDX, 1, bbuf);
309 if (rc != EOK)
310 return EIO;
311
312 printf("sysinst_copy_boot_blocks: write boot block\n");
313 /* XXX Must pad last block with zeros */
314 rc = block_write_direct(sid, core_start, core_blocks, core_img);
315 if (rc != EOK)
316 return EIO;
317
318 printf("sysinst_copy_boot_blocks: OK.\n");
319 return EOK;
320}
321
322/** Install system to a device.
323 *
324 * @param dev Device to install to.
325 * @return EOK on success or an error code
326 */
327static errno_t sysinst_install(const char *dev)
328{
329 errno_t rc;
330 char *pdev;
331
332 rc = sysinst_label_dev(dev, &pdev);
333 if (rc != EOK)
334 return rc;
335
336 printf("Partition '%s'. Mount it.\n", pdev);
337 rc = sysinst_fs_mount(pdev);
338 if (rc != EOK)
339 return rc;
340
341 free(pdev);
342
343 printf("FS created and mounted. Copying boot files.\n");
344 rc = sysinst_copy_boot_files();
345 if (rc != EOK)
346 return rc;
347
348 printf("Boot files done. Installing boot blocks.\n");
349 rc = sysinst_copy_boot_blocks(dev);
350 if (rc != EOK)
351 return rc;
352
353 return EOK;
354}
355
356int main(int argc, char *argv[])
357{
358 const char *dev = DEFAULT_DEV;
359 return sysinst_install(dev);
360}
361
362/** @}
363 */
Note: See TracBrowser for help on using the repository browser.