source: mainline/uspace/app/sysinst/sysinst.c@ 44428bb

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

Configuring mount point for (newly created) paritions.

  • 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 pspec.mountp = "";
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 if (asprintf(pdev, "%sp1", dev) < 0)
144 return ENOMEM;
145
146 printf("sysinst_label_dev(): OK\n");
147 return EOK;
148}
149
150/** Mount target file system.
151 *
152 * @param dev Partition device
153 * @return EOK on success or an error code
154 */
155static errno_t sysinst_fs_mount(const char *dev)
156{
157 task_wait_t twait;
158 task_exit_t texit;
159 errno_t rc;
160 int trc;
161
162 printf("sysinst_fs_mount(): start filesystem server\n");
163 rc = task_spawnl(NULL, &twait, FS_SRV, FS_SRV, NULL);
164 if (rc != EOK)
165 return rc;
166
167 printf("sysinst_fs_mount(): wait for filesystem server\n");
168 rc = task_wait(&twait, &texit, &trc);
169 if (rc != EOK)
170 return rc;
171
172 printf("sysinst_fs_mount(): verify filesystem server result\n");
173 if (texit != TASK_EXIT_NORMAL || trc != 0) {
174 printf("sysinst_fs_mount(): not successful, but could be already loaded.\n");
175 }
176
177 rc = vfs_link_path(MOUNT_POINT, KIND_DIRECTORY, NULL);
178 if (rc != EOK)
179 return rc;
180
181 printf("sysinst_fs_mount(): mount filesystem\n");
182 rc = vfs_mount_path(MOUNT_POINT, FS_TYPE, 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 an error code
193 */
194static errno_t sysinst_copy_boot_files(void)
195{
196 errno_t rc;
197
198 printf("sysinst_copy_boot_files(): copy bootloader files\n");
199 rc = futil_rcopy_contents(BOOT_FILES_SRC, MOUNT_POINT);
200 if (rc != EOK)
201 return rc;
202
203 printf("sysinst_copy_boot_files(): unmount %s\n", MOUNT_POINT);
204 rc = vfs_unmount_path(MOUNT_POINT);
205 if (rc != EOK)
206 return rc;
207
208 printf("sysinst_copy_boot_files(): OK\n");
209 return EOK;
210}
211
212/** Write unaligned 64-bit little-endian number.
213 *
214 * @param a Destination buffer
215 * @param data Number
216 */
217static void set_unaligned_u64le(uint8_t *a, uint64_t data)
218{
219 int i;
220
221 for (i = 0; i < 8; i++) {
222 a[i] = (data >> (i * 8)) & 0xff;
223 }
224}
225
226/** Copy boot blocks.
227 *
228 * Install Grub's boot blocks.
229 *
230 * @param devp Disk device
231 * @return EOK on success or an error code
232 */
233static errno_t sysinst_copy_boot_blocks(const char *devp)
234{
235 void *boot_img;
236 size_t boot_img_size;
237 void *core_img;
238 size_t core_img_size;
239 service_id_t sid;
240 size_t bsize;
241 uint8_t bbuf[512];
242 aoff64_t core_start;
243 aoff64_t core_blocks;
244 grub_boot_blocklist_t *first_bl, *bl;
245 errno_t rc;
246
247 printf("sysinst_copy_boot_blocks: Read boot block image.\n");
248 rc = futil_get_file(BOOT_FILES_SRC "/boot/grub/i386-pc/boot.img",
249 &boot_img, &boot_img_size);
250 if (rc != EOK || boot_img_size != 512)
251 return EIO;
252
253 printf("sysinst_copy_boot_blocks: Read GRUB core image.\n");
254 rc = futil_get_file(BOOT_FILES_SRC "/boot/grub/i386-pc/core.img",
255 &core_img, &core_img_size);
256 if (rc != EOK)
257 return EIO;
258
259 printf("sysinst_copy_boot_blocks: get service ID.\n");
260 rc = loc_service_get_id(devp, &sid, 0);
261 if (rc != EOK)
262 return rc;
263
264 printf("sysinst_copy_boot_blocks: block_init.\n");
265 rc = block_init(sid, 512);
266 if (rc != EOK)
267 return rc;
268
269 printf("sysinst_copy_boot_blocks: get block size\n");
270 rc = block_get_bsize(sid, &bsize);
271 if (rc != EOK)
272 return rc;
273
274 if (bsize != 512) {
275 printf("Device block size != 512.\n");
276 return EIO;
277 }
278
279 printf("sysinst_copy_boot_blocks: read boot block\n");
280 rc = block_read_direct(sid, BOOT_BLOCK_IDX, 1, bbuf);
281 if (rc != EOK)
282 return EIO;
283
284 core_start = 16;
285 core_blocks = (core_img_size + 511) / 512;
286
287 /* Clean blocklists */
288 first_bl = core_img + 512 - sizeof(*first_bl);
289 bl = first_bl;
290 while (bl->len != 0) {
291 memset(bl, 0, sizeof(*bl));
292 --bl;
293 if ((void *)bl < core_img) {
294 printf("No block terminator in core image.\n");
295 return EIO;
296 }
297 }
298
299 first_bl->start = host2uint64_t_le(core_start + 1);
300 first_bl->len = host2uint16_t_le(core_blocks - 1);
301 first_bl->segment = grub_boot_i386_pc_kernel_seg + (512 >> 4);
302
303 /* Write boot code into boot block */
304 memcpy(bbuf, boot_img, 440); /* XXX 440 = sizeof(br_block_t.code_area) */
305 bbuf[grub_boot_machine_boot_drive] = 0xff;
306 set_unaligned_u64le(bbuf + grub_boot_machine_kernel_sector, core_start);
307
308 printf("sysinst_copy_boot_blocks: write boot block\n");
309 rc = block_write_direct(sid, BOOT_BLOCK_IDX, 1, bbuf);
310 if (rc != EOK)
311 return EIO;
312
313 printf("sysinst_copy_boot_blocks: write boot block\n");
314 /* XXX Must pad last block with zeros */
315 rc = block_write_direct(sid, core_start, core_blocks, core_img);
316 if (rc != EOK)
317 return EIO;
318
319 printf("sysinst_copy_boot_blocks: OK.\n");
320 return EOK;
321}
322
323/** Install system to a device.
324 *
325 * @param dev Device to install to.
326 * @return EOK on success or an error code
327 */
328static errno_t sysinst_install(const char *dev)
329{
330 errno_t rc;
331 char *pdev;
332
333 rc = sysinst_label_dev(dev, &pdev);
334 if (rc != EOK)
335 return rc;
336
337 printf("Partition '%s'. Mount it.\n", pdev);
338 rc = sysinst_fs_mount(pdev);
339 if (rc != EOK)
340 return rc;
341
342 free(pdev);
343
344 printf("FS created and mounted. Copying boot files.\n");
345 rc = sysinst_copy_boot_files();
346 if (rc != EOK)
347 return rc;
348
349 printf("Boot files done. Installing boot blocks.\n");
350 rc = sysinst_copy_boot_blocks(dev);
351 if (rc != EOK)
352 return rc;
353
354 return EOK;
355}
356
357int main(int argc, char *argv[])
358{
359 const char *dev = DEFAULT_DEV;
360 return sysinst_install(dev);
361}
362
363/** @}
364 */
Note: See TracBrowser for help on using the repository browser.