cd9660_eltorito.c revision 1.13 1 1.13 christos /* $NetBSD: cd9660_eltorito.c,v 1.13 2010/10/22 00:49:15 christos Exp $ */
2 1.1 fvdl
3 1.1 fvdl /*
4 1.1 fvdl * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
5 1.1 fvdl * Perez-Rathke and Ram Vedam. All rights reserved.
6 1.1 fvdl *
7 1.1 fvdl * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
8 1.1 fvdl * Alan Perez-Rathke and Ram Vedam.
9 1.1 fvdl *
10 1.1 fvdl * Redistribution and use in source and binary forms, with or
11 1.1 fvdl * without modification, are permitted provided that the following
12 1.1 fvdl * conditions are met:
13 1.1 fvdl * 1. Redistributions of source code must retain the above copyright
14 1.1 fvdl * notice, this list of conditions and the following disclaimer.
15 1.1 fvdl * 2. Redistributions in binary form must reproduce the above
16 1.1 fvdl * copyright notice, this list of conditions and the following
17 1.1 fvdl * disclaimer in the documentation and/or other materials provided
18 1.1 fvdl * with the distribution.
19 1.1 fvdl *
20 1.1 fvdl * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
21 1.1 fvdl * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
22 1.1 fvdl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 1.5 dyoung * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 1.1 fvdl * DISCLAIMED. IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
25 1.1 fvdl * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
26 1.1 fvdl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 1.1 fvdl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
28 1.1 fvdl * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29 1.1 fvdl * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 1.1 fvdl * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 1.1 fvdl * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
32 1.1 fvdl * OF SUCH DAMAGE.
33 1.1 fvdl */
34 1.1 fvdl #include "cd9660.h"
35 1.1 fvdl #include "cd9660_eltorito.h"
36 1.1 fvdl
37 1.1 fvdl #include <sys/cdefs.h>
38 1.1 fvdl #if defined(__RCSID) && !defined(__lint)
39 1.13 christos __RCSID("$NetBSD: cd9660_eltorito.c,v 1.13 2010/10/22 00:49:15 christos Exp $");
40 1.1 fvdl #endif /* !__lint */
41 1.1 fvdl
42 1.7 dyoung #ifdef DEBUG
43 1.7 dyoung #define ELTORITO_DPRINTF(__x) printf __x
44 1.7 dyoung #else
45 1.7 dyoung #define ELTORITO_DPRINTF(__x)
46 1.7 dyoung #endif
47 1.7 dyoung
48 1.1 fvdl static struct boot_catalog_entry *cd9660_init_boot_catalog_entry(void);
49 1.1 fvdl static struct boot_catalog_entry *cd9660_boot_setup_validation_entry(char);
50 1.1 fvdl static struct boot_catalog_entry *cd9660_boot_setup_default_entry(
51 1.1 fvdl struct cd9660_boot_image *);
52 1.1 fvdl static struct boot_catalog_entry *cd9660_boot_setup_section_head(char);
53 1.1 fvdl static struct boot_catalog_entry *cd9660_boot_setup_validation_entry(char);
54 1.1 fvdl #if 0
55 1.1 fvdl static u_char cd9660_boot_get_system_type(struct cd9660_boot_image *);
56 1.1 fvdl #endif
57 1.1 fvdl
58 1.1 fvdl int
59 1.6 dyoung cd9660_add_boot_disk(const char *boot_info)
60 1.1 fvdl {
61 1.1 fvdl struct stat stbuf;
62 1.9 dyoung const char *mode_msg;
63 1.1 fvdl char *temp;
64 1.1 fvdl char *sysname;
65 1.1 fvdl char *filename;
66 1.8 dyoung struct cd9660_boot_image *new_image, *tmp_image;
67 1.5 dyoung
68 1.1 fvdl assert(boot_info != NULL);
69 1.1 fvdl
70 1.2 dyoung if (*boot_info == '\0') {
71 1.2 dyoung warnx("Error: Boot disk information must be in the "
72 1.2 dyoung "format 'system;filename'");
73 1.5 dyoung return 0;
74 1.2 dyoung }
75 1.2 dyoung
76 1.1 fvdl /* First decode the boot information */
77 1.2 dyoung if ((temp = strdup(boot_info)) == NULL) {
78 1.2 dyoung warn("%s: strdup", __func__);
79 1.1 fvdl return 0;
80 1.1 fvdl }
81 1.1 fvdl
82 1.1 fvdl sysname = temp;
83 1.1 fvdl filename = strchr(sysname, ';');
84 1.1 fvdl if (filename == NULL) {
85 1.2 dyoung warnx("supply boot disk information in the format "
86 1.2 dyoung "'system;filename'");
87 1.11 christos free(temp);
88 1.5 dyoung return 0;
89 1.1 fvdl }
90 1.1 fvdl
91 1.2 dyoung *filename++ = '\0';
92 1.2 dyoung
93 1.9 dyoung if (diskStructure.verbose_level > 0) {
94 1.9 dyoung printf("Found bootdisk with system %s, and filename %s\n",
95 1.9 dyoung sysname, filename);
96 1.9 dyoung }
97 1.7 dyoung if ((new_image = malloc(sizeof(*new_image))) == NULL) {
98 1.2 dyoung warn("%s: malloc", __func__);
99 1.11 christos free(temp);
100 1.1 fvdl return 0;
101 1.1 fvdl }
102 1.7 dyoung (void)memset(new_image, 0, sizeof(*new_image));
103 1.1 fvdl new_image->loadSegment = 0; /* default for now */
104 1.1 fvdl
105 1.1 fvdl /* Decode System */
106 1.2 dyoung if (strcmp(sysname, "i386") == 0)
107 1.1 fvdl new_image->system = ET_SYS_X86;
108 1.2 dyoung else if (strcmp(sysname, "powerpc") == 0)
109 1.1 fvdl new_image->system = ET_SYS_PPC;
110 1.4 dyoung else if (strcmp(sysname, "macppc") == 0 ||
111 1.4 dyoung strcmp(sysname, "mac68k") == 0)
112 1.1 fvdl new_image->system = ET_SYS_MAC;
113 1.1 fvdl else {
114 1.2 dyoung warnx("boot disk system must be "
115 1.2 dyoung "i386, powerpc, macppc, or mac68k");
116 1.11 christos free(temp);
117 1.11 christos free(new_image);
118 1.1 fvdl return 0;
119 1.1 fvdl }
120 1.5 dyoung
121 1.5 dyoung
122 1.2 dyoung if ((new_image->filename = strdup(filename)) == NULL) {
123 1.2 dyoung warn("%s: strdup", __func__);
124 1.11 christos free(temp);
125 1.11 christos free(new_image);
126 1.1 fvdl return 0;
127 1.1 fvdl }
128 1.1 fvdl
129 1.1 fvdl free(temp);
130 1.5 dyoung
131 1.1 fvdl /* Get information about the file */
132 1.1 fvdl if (lstat(new_image->filename, &stbuf) == -1)
133 1.2 dyoung err(EXIT_FAILURE, "%s: lstat(\"%s\")", __func__,
134 1.2 dyoung new_image->filename);
135 1.1 fvdl
136 1.1 fvdl switch (stbuf.st_size) {
137 1.1 fvdl case 1440 * 1024:
138 1.1 fvdl new_image->targetMode = ET_MEDIA_144FDD;
139 1.9 dyoung mode_msg = "Assigned boot image to 1.44 emulation mode";
140 1.1 fvdl break;
141 1.1 fvdl case 1200 * 1024:
142 1.1 fvdl new_image->targetMode = ET_MEDIA_12FDD;
143 1.9 dyoung mode_msg = "Assigned boot image to 1.2 emulation mode";
144 1.1 fvdl break;
145 1.1 fvdl case 2880 * 1024:
146 1.1 fvdl new_image->targetMode = ET_MEDIA_288FDD;
147 1.9 dyoung mode_msg = "Assigned boot image to 2.88 emulation mode";
148 1.1 fvdl break;
149 1.1 fvdl default:
150 1.1 fvdl new_image->targetMode = ET_MEDIA_NOEM;
151 1.9 dyoung mode_msg = "Assigned boot image to no emulation mode";
152 1.1 fvdl break;
153 1.1 fvdl }
154 1.5 dyoung
155 1.9 dyoung if (diskStructure.verbose_level > 0)
156 1.9 dyoung printf("%s\n", mode_msg);
157 1.9 dyoung
158 1.1 fvdl new_image->size = stbuf.st_size;
159 1.1 fvdl new_image->num_sectors =
160 1.7 dyoung howmany(new_image->size, diskStructure.sectorSize) *
161 1.7 dyoung howmany(diskStructure.sectorSize, 512);
162 1.9 dyoung if (diskStructure.verbose_level > 0) {
163 1.9 dyoung printf("New image has size %d, uses %d 512-byte sectors\n",
164 1.9 dyoung new_image->size, new_image->num_sectors);
165 1.9 dyoung }
166 1.1 fvdl new_image->sector = -1;
167 1.1 fvdl /* Bootable by default */
168 1.1 fvdl new_image->bootable = ET_BOOTABLE;
169 1.1 fvdl /* Add boot disk */
170 1.1 fvdl
171 1.8 dyoung /* Group images for the same platform together. */
172 1.8 dyoung TAILQ_FOREACH(tmp_image, &diskStructure.boot_images, image_list) {
173 1.8 dyoung if (tmp_image->system != new_image->system)
174 1.8 dyoung break;
175 1.8 dyoung }
176 1.8 dyoung
177 1.8 dyoung if (tmp_image == NULL) {
178 1.7 dyoung TAILQ_INSERT_HEAD(&diskStructure.boot_images, new_image,
179 1.1 fvdl image_list);
180 1.8 dyoung } else
181 1.8 dyoung TAILQ_INSERT_BEFORE(tmp_image, new_image, image_list);
182 1.8 dyoung
183 1.8 dyoung new_image->serialno = diskStructure.image_serialno++;
184 1.5 dyoung
185 1.1 fvdl /* TODO : Need to do anything about the boot image in the tree? */
186 1.1 fvdl diskStructure.is_bootable = 1;
187 1.5 dyoung
188 1.1 fvdl return 1;
189 1.1 fvdl }
190 1.1 fvdl
191 1.1 fvdl int
192 1.6 dyoung cd9660_eltorito_add_boot_option(const char *option_string, const char *value)
193 1.1 fvdl {
194 1.7 dyoung char *eptr;
195 1.1 fvdl struct cd9660_boot_image *image;
196 1.5 dyoung
197 1.1 fvdl assert(option_string != NULL);
198 1.5 dyoung
199 1.1 fvdl /* Find the last image added */
200 1.8 dyoung TAILQ_FOREACH(image, &diskStructure.boot_images, image_list) {
201 1.8 dyoung if (image->serialno + 1 == diskStructure.image_serialno)
202 1.8 dyoung break;
203 1.8 dyoung }
204 1.1 fvdl if (image == NULL)
205 1.2 dyoung errx(EXIT_FAILURE, "Attempted to add boot option, "
206 1.2 dyoung "but no boot images have been specified");
207 1.1 fvdl
208 1.1 fvdl if (strcmp(option_string, "no-emul-boot") == 0) {
209 1.5 dyoung image->targetMode = ET_MEDIA_NOEM;
210 1.1 fvdl } else if (strcmp(option_string, "no-boot") == 0) {
211 1.1 fvdl image->bootable = ET_NOT_BOOTABLE;
212 1.1 fvdl } else if (strcmp(option_string, "hard-disk-boot") == 0) {
213 1.1 fvdl image->targetMode = ET_MEDIA_HDD;
214 1.1 fvdl } else if (strcmp(option_string, "boot-load-segment") == 0) {
215 1.7 dyoung image->loadSegment = strtoul(value, &eptr, 16);
216 1.7 dyoung if (eptr == value || *eptr != '\0' || errno != ERANGE) {
217 1.7 dyoung warn("%s: strtoul", __func__);
218 1.7 dyoung return 0;
219 1.7 dyoung }
220 1.1 fvdl } else {
221 1.5 dyoung return 0;
222 1.1 fvdl }
223 1.1 fvdl return 1;
224 1.1 fvdl }
225 1.1 fvdl
226 1.1 fvdl static struct boot_catalog_entry *
227 1.1 fvdl cd9660_init_boot_catalog_entry(void)
228 1.1 fvdl {
229 1.1 fvdl struct boot_catalog_entry *temp;
230 1.1 fvdl
231 1.7 dyoung if ((temp = malloc(sizeof(*temp))) == NULL)
232 1.7 dyoung return NULL;
233 1.7 dyoung
234 1.7 dyoung return memset(temp, 0, sizeof(*temp));
235 1.1 fvdl }
236 1.1 fvdl
237 1.1 fvdl static struct boot_catalog_entry *
238 1.1 fvdl cd9660_boot_setup_validation_entry(char sys)
239 1.1 fvdl {
240 1.6 dyoung struct boot_catalog_entry *entry;
241 1.6 dyoung boot_catalog_validation_entry *ve;
242 1.1 fvdl int16_t checksum;
243 1.1 fvdl unsigned char *csptr;
244 1.1 fvdl int i;
245 1.6 dyoung entry = cd9660_init_boot_catalog_entry();
246 1.5 dyoung
247 1.6 dyoung if (entry == NULL) {
248 1.1 fvdl warnx("Error: memory allocation failed in "
249 1.1 fvdl "cd9660_boot_setup_validation_entry");
250 1.1 fvdl return 0;
251 1.1 fvdl }
252 1.6 dyoung ve = &entry->entry_data.VE;
253 1.6 dyoung
254 1.6 dyoung ve->header_id[0] = 1;
255 1.6 dyoung ve->platform_id[0] = sys;
256 1.6 dyoung ve->key[0] = 0x55;
257 1.6 dyoung ve->key[1] = 0xAA;
258 1.1 fvdl
259 1.1 fvdl /* Calculate checksum */
260 1.1 fvdl checksum = 0;
261 1.6 dyoung cd9660_721(0, ve->checksum);
262 1.7 dyoung csptr = (unsigned char*)ve;
263 1.6 dyoung for (i = 0; i < sizeof(*ve); i += 2) {
264 1.6 dyoung checksum += (int16_t)csptr[i];
265 1.6 dyoung checksum += 256 * (int16_t)csptr[i + 1];
266 1.1 fvdl }
267 1.1 fvdl checksum = -checksum;
268 1.6 dyoung cd9660_721(checksum, ve->checksum);
269 1.5 dyoung
270 1.7 dyoung ELTORITO_DPRINTF(("%s: header_id %d, platform_id %d, key[0] %d, key[1] %d, "
271 1.7 dyoung "checksum %04x\n", __func__, ve->header_id[0], ve->platform_id[0],
272 1.7 dyoung ve->key[0], ve->key[1], checksum));
273 1.6 dyoung return entry;
274 1.1 fvdl }
275 1.1 fvdl
276 1.1 fvdl static struct boot_catalog_entry *
277 1.1 fvdl cd9660_boot_setup_default_entry(struct cd9660_boot_image *disk)
278 1.1 fvdl {
279 1.1 fvdl struct boot_catalog_entry *default_entry;
280 1.6 dyoung boot_catalog_initial_entry *ie;
281 1.1 fvdl
282 1.1 fvdl default_entry = cd9660_init_boot_catalog_entry();
283 1.1 fvdl if (default_entry == NULL)
284 1.1 fvdl return NULL;
285 1.5 dyoung
286 1.6 dyoung ie = &default_entry->entry_data.IE;
287 1.5 dyoung
288 1.6 dyoung ie->boot_indicator[0] = disk->bootable;
289 1.6 dyoung ie->media_type[0] = disk->targetMode;
290 1.6 dyoung cd9660_721(disk->loadSegment, ie->load_segment);
291 1.6 dyoung ie->system_type[0] = disk->system;
292 1.7 dyoung cd9660_721(disk->num_sectors, ie->sector_count);
293 1.6 dyoung cd9660_731(disk->sector, ie->load_rba);
294 1.5 dyoung
295 1.7 dyoung ELTORITO_DPRINTF(("%s: boot indicator %d, media type %d, "
296 1.7 dyoung "load segment %04x, system type %d, sector count %d, "
297 1.7 dyoung "load rba %d\n", __func__, ie->boot_indicator[0],
298 1.7 dyoung ie->media_type[0], disk->loadSegment, ie->system_type[0],
299 1.7 dyoung disk->num_sectors, disk->sector));
300 1.1 fvdl return default_entry;
301 1.1 fvdl }
302 1.1 fvdl
303 1.1 fvdl static struct boot_catalog_entry *
304 1.1 fvdl cd9660_boot_setup_section_head(char platform)
305 1.1 fvdl {
306 1.6 dyoung struct boot_catalog_entry *entry;
307 1.6 dyoung boot_catalog_section_header *sh;
308 1.1 fvdl
309 1.6 dyoung entry = cd9660_init_boot_catalog_entry();
310 1.6 dyoung if (entry == NULL)
311 1.1 fvdl return NULL;
312 1.6 dyoung
313 1.6 dyoung sh = &entry->entry_data.SH;
314 1.1 fvdl /* More by default. The last one will manually be set to 0x91 */
315 1.6 dyoung sh->header_indicator[0] = ET_SECTION_HEADER_MORE;
316 1.6 dyoung sh->platform_id[0] = platform;
317 1.6 dyoung sh->num_section_entries[0] = 0;
318 1.6 dyoung return entry;
319 1.1 fvdl }
320 1.1 fvdl
321 1.1 fvdl static struct boot_catalog_entry *
322 1.1 fvdl cd9660_boot_setup_section_entry(struct cd9660_boot_image *disk)
323 1.1 fvdl {
324 1.1 fvdl struct boot_catalog_entry *entry;
325 1.6 dyoung boot_catalog_section_entry *se;
326 1.1 fvdl if ((entry = cd9660_init_boot_catalog_entry()) == NULL)
327 1.1 fvdl return NULL;
328 1.5 dyoung
329 1.6 dyoung se = &entry->entry_data.SE;
330 1.6 dyoung
331 1.6 dyoung se->boot_indicator[0] = ET_BOOTABLE;
332 1.6 dyoung se->media_type[0] = disk->targetMode;
333 1.6 dyoung cd9660_721(disk->loadSegment, se->load_segment);
334 1.7 dyoung cd9660_721(disk->num_sectors, se->sector_count);
335 1.7 dyoung cd9660_731(disk->sector, se->load_rba);
336 1.1 fvdl return entry;
337 1.1 fvdl }
338 1.1 fvdl
339 1.1 fvdl #if 0
340 1.1 fvdl static u_char
341 1.1 fvdl cd9660_boot_get_system_type(struct cd9660_boot_image *disk)
342 1.1 fvdl {
343 1.1 fvdl /*
344 1.1 fvdl For hard drive booting, we need to examine the MBR to figure
345 1.1 fvdl out what the partition type is
346 1.1 fvdl */
347 1.1 fvdl return 0;
348 1.1 fvdl }
349 1.1 fvdl #endif
350 1.1 fvdl
351 1.1 fvdl /*
352 1.1 fvdl * Set up the BVD, Boot catalog, and the boot entries, but do no writing
353 1.1 fvdl */
354 1.1 fvdl int
355 1.1 fvdl cd9660_setup_boot(int first_sector)
356 1.1 fvdl {
357 1.1 fvdl int sector;
358 1.1 fvdl int used_sectors;
359 1.1 fvdl int num_entries = 0;
360 1.1 fvdl int catalog_sectors;
361 1.1 fvdl struct boot_catalog_entry *x86_head, *mac_head, *ppc_head,
362 1.8 dyoung *valid_entry, *default_entry, *temp, *head, **headp, *next;
363 1.1 fvdl struct cd9660_boot_image *tmp_disk;
364 1.1 fvdl
365 1.8 dyoung headp = NULL;
366 1.8 dyoung x86_head = mac_head = ppc_head = NULL;
367 1.5 dyoung
368 1.1 fvdl /* If there are no boot disks, don't bother building boot information */
369 1.8 dyoung if (TAILQ_EMPTY(&diskStructure.boot_images))
370 1.1 fvdl return 0;
371 1.1 fvdl
372 1.1 fvdl /* Point to catalog: For now assume it consumes one sector */
373 1.9 dyoung ELTORITO_DPRINTF(("Boot catalog will go in sector %d\n", first_sector));
374 1.1 fvdl diskStructure.boot_catalog_sector = first_sector;
375 1.1 fvdl cd9660_bothendian_dword(first_sector,
376 1.1 fvdl diskStructure.boot_descriptor->boot_catalog_pointer);
377 1.5 dyoung
378 1.1 fvdl /* Step 1: Generate boot catalog */
379 1.1 fvdl /* Step 1a: Validation entry */
380 1.1 fvdl valid_entry = cd9660_boot_setup_validation_entry(ET_SYS_X86);
381 1.1 fvdl if (valid_entry == NULL)
382 1.1 fvdl return -1;
383 1.5 dyoung
384 1.1 fvdl /*
385 1.1 fvdl * Count how many boot images there are,
386 1.1 fvdl * and how many sectors they consume.
387 1.1 fvdl */
388 1.1 fvdl num_entries = 1;
389 1.1 fvdl used_sectors = 0;
390 1.5 dyoung
391 1.7 dyoung TAILQ_FOREACH(tmp_disk, &diskStructure.boot_images, image_list) {
392 1.1 fvdl used_sectors += tmp_disk->num_sectors;
393 1.5 dyoung
394 1.1 fvdl /* One default entry per image */
395 1.1 fvdl num_entries++;
396 1.1 fvdl }
397 1.1 fvdl catalog_sectors = howmany(num_entries * 0x20, diskStructure.sectorSize);
398 1.1 fvdl used_sectors += catalog_sectors;
399 1.5 dyoung
400 1.9 dyoung if (diskStructure.verbose_level > 0) {
401 1.9 dyoung printf("%s: there will be %i entries consuming %i sectors. "
402 1.9 dyoung "Catalog is %i sectors\n", __func__, num_entries,
403 1.9 dyoung used_sectors, catalog_sectors);
404 1.9 dyoung }
405 1.5 dyoung
406 1.1 fvdl /* Populate sector numbers */
407 1.1 fvdl sector = first_sector + catalog_sectors;
408 1.7 dyoung TAILQ_FOREACH(tmp_disk, &diskStructure.boot_images, image_list) {
409 1.1 fvdl tmp_disk->sector = sector;
410 1.1 fvdl sector += tmp_disk->num_sectors;
411 1.1 fvdl }
412 1.5 dyoung
413 1.1 fvdl LIST_INSERT_HEAD(&diskStructure.boot_entries, valid_entry, ll_struct);
414 1.5 dyoung
415 1.1 fvdl /* Step 1b: Initial/default entry */
416 1.1 fvdl /* TODO : PARAM */
417 1.7 dyoung tmp_disk = TAILQ_FIRST(&diskStructure.boot_images);
418 1.1 fvdl default_entry = cd9660_boot_setup_default_entry(tmp_disk);
419 1.1 fvdl if (default_entry == NULL) {
420 1.1 fvdl warnx("Error: memory allocation failed in cd9660_setup_boot");
421 1.1 fvdl return -1;
422 1.1 fvdl }
423 1.5 dyoung
424 1.1 fvdl LIST_INSERT_AFTER(valid_entry, default_entry, ll_struct);
425 1.5 dyoung
426 1.1 fvdl /* Todo: multiple default entries? */
427 1.5 dyoung
428 1.7 dyoung tmp_disk = TAILQ_NEXT(tmp_disk, image_list);
429 1.5 dyoung
430 1.1 fvdl temp = default_entry;
431 1.5 dyoung
432 1.1 fvdl /* If multiple boot images are given : */
433 1.1 fvdl while (tmp_disk != NULL) {
434 1.1 fvdl /* Step 2: Section header */
435 1.1 fvdl switch (tmp_disk->system) {
436 1.1 fvdl case ET_SYS_X86:
437 1.8 dyoung headp = &x86_head;
438 1.1 fvdl break;
439 1.1 fvdl case ET_SYS_PPC:
440 1.8 dyoung headp = &ppc_head;
441 1.1 fvdl break;
442 1.1 fvdl case ET_SYS_MAC:
443 1.8 dyoung headp = &mac_head;
444 1.1 fvdl break;
445 1.8 dyoung default:
446 1.8 dyoung warnx("%s: internal error: unknown system type",
447 1.8 dyoung __func__);
448 1.8 dyoung return -1;
449 1.1 fvdl }
450 1.1 fvdl
451 1.8 dyoung if (*headp == NULL) {
452 1.8 dyoung head =
453 1.5 dyoung cd9660_boot_setup_section_head(tmp_disk->system);
454 1.8 dyoung if (head == NULL) {
455 1.1 fvdl warnx("Error: memory allocation failed in "
456 1.1 fvdl "cd9660_setup_boot");
457 1.1 fvdl return -1;
458 1.1 fvdl }
459 1.8 dyoung LIST_INSERT_AFTER(default_entry, head, ll_struct);
460 1.8 dyoung *headp = head;
461 1.8 dyoung } else
462 1.8 dyoung head = *headp;
463 1.8 dyoung
464 1.8 dyoung head->entry_data.SH.num_section_entries[0]++;
465 1.5 dyoung
466 1.1 fvdl /* Step 2a: Section entry and extensions */
467 1.1 fvdl temp = cd9660_boot_setup_section_entry(tmp_disk);
468 1.1 fvdl if (temp == NULL) {
469 1.8 dyoung warn("%s: cd9660_boot_setup_section_entry", __func__);
470 1.1 fvdl return -1;
471 1.1 fvdl }
472 1.1 fvdl
473 1.8 dyoung while ((next = LIST_NEXT(head, ll_struct)) != NULL &&
474 1.3 dyoung next->entry_type == ET_ENTRY_SE)
475 1.8 dyoung head = next;
476 1.5 dyoung
477 1.8 dyoung LIST_INSERT_AFTER(head, temp, ll_struct);
478 1.7 dyoung tmp_disk = TAILQ_NEXT(tmp_disk, image_list);
479 1.1 fvdl }
480 1.5 dyoung
481 1.1 fvdl /* TODO: Remaining boot disks when implemented */
482 1.5 dyoung
483 1.1 fvdl return first_sector + used_sectors;
484 1.1 fvdl }
485 1.1 fvdl
486 1.1 fvdl int
487 1.12 reinoud cd9660_setup_boot_volume_descriptor(volume_descriptor *bvd)
488 1.1 fvdl {
489 1.1 fvdl boot_volume_descriptor *bvdData =
490 1.1 fvdl (boot_volume_descriptor*)bvd->volumeDescriptorData;
491 1.1 fvdl
492 1.1 fvdl bvdData->boot_record_indicator[0] = ISO_VOLUME_DESCRIPTOR_BOOT;
493 1.1 fvdl memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
494 1.1 fvdl bvdData->version[0] = 1;
495 1.1 fvdl memcpy(bvdData->boot_system_identifier, ET_ID, 23);
496 1.1 fvdl memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
497 1.1 fvdl diskStructure.boot_descriptor =
498 1.1 fvdl (boot_volume_descriptor*) bvd->volumeDescriptorData;
499 1.1 fvdl return 1;
500 1.1 fvdl }
501 1.1 fvdl
502 1.5 dyoung int
503 1.1 fvdl cd9660_write_boot(FILE *fd)
504 1.1 fvdl {
505 1.1 fvdl struct boot_catalog_entry *e;
506 1.1 fvdl struct cd9660_boot_image *t;
507 1.1 fvdl
508 1.1 fvdl /* write boot catalog */
509 1.13 christos if (fseeko(fd, (off_t)diskStructure.boot_catalog_sector *
510 1.13 christos diskStructure.sectorSize, SEEK_SET) == -1)
511 1.13 christos err(1, "fseeko");
512 1.5 dyoung
513 1.9 dyoung if (diskStructure.verbose_level > 0) {
514 1.9 dyoung printf("Writing boot catalog to sector %d\n",
515 1.9 dyoung diskStructure.boot_catalog_sector);
516 1.9 dyoung }
517 1.3 dyoung LIST_FOREACH(e, &diskStructure.boot_entries, ll_struct) {
518 1.9 dyoung if (diskStructure.verbose_level > 0) {
519 1.9 dyoung printf("Writing catalog entry of type %d\n",
520 1.9 dyoung e->entry_type);
521 1.9 dyoung }
522 1.1 fvdl /*
523 1.1 fvdl * It doesnt matter which one gets written
524 1.1 fvdl * since they are the same size
525 1.1 fvdl */
526 1.1 fvdl fwrite(&(e->entry_data.VE), 1, 32, fd);
527 1.1 fvdl }
528 1.10 dyoung if (diskStructure.verbose_level > 0)
529 1.10 dyoung printf("Finished writing boot catalog\n");
530 1.5 dyoung
531 1.1 fvdl /* copy boot images */
532 1.7 dyoung TAILQ_FOREACH(t, &diskStructure.boot_images, image_list) {
533 1.9 dyoung if (diskStructure.verbose_level > 0) {
534 1.9 dyoung printf("Writing boot image from %s to sectors %d\n",
535 1.9 dyoung t->filename, t->sector);
536 1.9 dyoung }
537 1.1 fvdl cd9660_copy_file(fd, t->sector, t->filename);
538 1.1 fvdl }
539 1.1 fvdl
540 1.1 fvdl return 0;
541 1.1 fvdl }
542