source: mainline/uspace/app/sysinst/sysinst.c@ 10cb47e

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

Simple system installer.

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