cd9660_eltorito.c revision 1.19.2.1 1 1.19.2.1 tls /* $NetBSD: cd9660_eltorito.c,v 1.19.2.1 2013/02/25 00:30:44 tls 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.15 christos
35 1.15 christos
36 1.1 fvdl #include "cd9660.h"
37 1.1 fvdl #include "cd9660_eltorito.h"
38 1.18 christos #include <sys/bootblock.h>
39 1.19.2.1 tls #include <util.h>
40 1.1 fvdl
41 1.1 fvdl #include <sys/cdefs.h>
42 1.1 fvdl #if defined(__RCSID) && !defined(__lint)
43 1.19.2.1 tls __RCSID("$NetBSD: cd9660_eltorito.c,v 1.19.2.1 2013/02/25 00:30:44 tls Exp $");
44 1.1 fvdl #endif /* !__lint */
45 1.1 fvdl
46 1.7 dyoung #ifdef DEBUG
47 1.7 dyoung #define ELTORITO_DPRINTF(__x) printf __x
48 1.7 dyoung #else
49 1.7 dyoung #define ELTORITO_DPRINTF(__x)
50 1.7 dyoung #endif
51 1.7 dyoung
52 1.19.2.1 tls #include <util.h>
53 1.19.2.1 tls
54 1.1 fvdl static struct boot_catalog_entry *cd9660_init_boot_catalog_entry(void);
55 1.1 fvdl static struct boot_catalog_entry *cd9660_boot_setup_validation_entry(char);
56 1.1 fvdl static struct boot_catalog_entry *cd9660_boot_setup_default_entry(
57 1.1 fvdl struct cd9660_boot_image *);
58 1.1 fvdl static struct boot_catalog_entry *cd9660_boot_setup_section_head(char);
59 1.1 fvdl static struct boot_catalog_entry *cd9660_boot_setup_validation_entry(char);
60 1.1 fvdl #if 0
61 1.1 fvdl static u_char cd9660_boot_get_system_type(struct cd9660_boot_image *);
62 1.1 fvdl #endif
63 1.1 fvdl
64 1.1 fvdl int
65 1.19.2.1 tls cd9660_add_boot_disk(iso9660_disk *diskStructure, const char *boot_info)
66 1.1 fvdl {
67 1.1 fvdl struct stat stbuf;
68 1.9 dyoung const char *mode_msg;
69 1.1 fvdl char *temp;
70 1.1 fvdl char *sysname;
71 1.1 fvdl char *filename;
72 1.8 dyoung struct cd9660_boot_image *new_image, *tmp_image;
73 1.5 dyoung
74 1.1 fvdl assert(boot_info != NULL);
75 1.1 fvdl
76 1.2 dyoung if (*boot_info == '\0') {
77 1.2 dyoung warnx("Error: Boot disk information must be in the "
78 1.2 dyoung "format 'system;filename'");
79 1.5 dyoung return 0;
80 1.2 dyoung }
81 1.2 dyoung
82 1.1 fvdl /* First decode the boot information */
83 1.19.2.1 tls temp = estrdup(boot_info);
84 1.1 fvdl
85 1.1 fvdl sysname = temp;
86 1.1 fvdl filename = strchr(sysname, ';');
87 1.1 fvdl if (filename == NULL) {
88 1.2 dyoung warnx("supply boot disk information in the format "
89 1.2 dyoung "'system;filename'");
90 1.11 christos free(temp);
91 1.5 dyoung return 0;
92 1.1 fvdl }
93 1.1 fvdl
94 1.2 dyoung *filename++ = '\0';
95 1.2 dyoung
96 1.19.2.1 tls if (diskStructure->verbose_level > 0) {
97 1.9 dyoung printf("Found bootdisk with system %s, and filename %s\n",
98 1.9 dyoung sysname, filename);
99 1.9 dyoung }
100 1.19.2.1 tls new_image = ecalloc(1, sizeof(*new_image));
101 1.1 fvdl new_image->loadSegment = 0; /* default for now */
102 1.1 fvdl
103 1.1 fvdl /* Decode System */
104 1.2 dyoung if (strcmp(sysname, "i386") == 0)
105 1.1 fvdl new_image->system = ET_SYS_X86;
106 1.2 dyoung else if (strcmp(sysname, "powerpc") == 0)
107 1.1 fvdl new_image->system = ET_SYS_PPC;
108 1.4 dyoung else if (strcmp(sysname, "macppc") == 0 ||
109 1.4 dyoung strcmp(sysname, "mac68k") == 0)
110 1.1 fvdl new_image->system = ET_SYS_MAC;
111 1.1 fvdl else {
112 1.2 dyoung warnx("boot disk system must be "
113 1.2 dyoung "i386, powerpc, macppc, or mac68k");
114 1.11 christos free(temp);
115 1.11 christos free(new_image);
116 1.1 fvdl return 0;
117 1.1 fvdl }
118 1.5 dyoung
119 1.5 dyoung
120 1.19.2.1 tls new_image->filename = estrdup(filename);
121 1.1 fvdl
122 1.1 fvdl free(temp);
123 1.5 dyoung
124 1.1 fvdl /* Get information about the file */
125 1.1 fvdl if (lstat(new_image->filename, &stbuf) == -1)
126 1.2 dyoung err(EXIT_FAILURE, "%s: lstat(\"%s\")", __func__,
127 1.2 dyoung new_image->filename);
128 1.1 fvdl
129 1.1 fvdl switch (stbuf.st_size) {
130 1.1 fvdl case 1440 * 1024:
131 1.1 fvdl new_image->targetMode = ET_MEDIA_144FDD;
132 1.9 dyoung mode_msg = "Assigned boot image to 1.44 emulation mode";
133 1.1 fvdl break;
134 1.1 fvdl case 1200 * 1024:
135 1.1 fvdl new_image->targetMode = ET_MEDIA_12FDD;
136 1.9 dyoung mode_msg = "Assigned boot image to 1.2 emulation mode";
137 1.1 fvdl break;
138 1.1 fvdl case 2880 * 1024:
139 1.1 fvdl new_image->targetMode = ET_MEDIA_288FDD;
140 1.9 dyoung mode_msg = "Assigned boot image to 2.88 emulation mode";
141 1.1 fvdl break;
142 1.1 fvdl default:
143 1.1 fvdl new_image->targetMode = ET_MEDIA_NOEM;
144 1.9 dyoung mode_msg = "Assigned boot image to no emulation mode";
145 1.1 fvdl break;
146 1.1 fvdl }
147 1.5 dyoung
148 1.19.2.1 tls if (diskStructure->verbose_level > 0)
149 1.9 dyoung printf("%s\n", mode_msg);
150 1.9 dyoung
151 1.1 fvdl new_image->size = stbuf.st_size;
152 1.1 fvdl new_image->num_sectors =
153 1.19.2.1 tls howmany(new_image->size, diskStructure->sectorSize) *
154 1.19.2.1 tls howmany(diskStructure->sectorSize, 512);
155 1.19.2.1 tls if (diskStructure->verbose_level > 0) {
156 1.9 dyoung printf("New image has size %d, uses %d 512-byte sectors\n",
157 1.9 dyoung new_image->size, new_image->num_sectors);
158 1.9 dyoung }
159 1.1 fvdl new_image->sector = -1;
160 1.1 fvdl /* Bootable by default */
161 1.1 fvdl new_image->bootable = ET_BOOTABLE;
162 1.1 fvdl /* Add boot disk */
163 1.1 fvdl
164 1.8 dyoung /* Group images for the same platform together. */
165 1.19.2.1 tls TAILQ_FOREACH(tmp_image, &diskStructure->boot_images, image_list) {
166 1.8 dyoung if (tmp_image->system != new_image->system)
167 1.8 dyoung break;
168 1.8 dyoung }
169 1.8 dyoung
170 1.8 dyoung if (tmp_image == NULL) {
171 1.19.2.1 tls TAILQ_INSERT_HEAD(&diskStructure->boot_images, new_image,
172 1.1 fvdl image_list);
173 1.8 dyoung } else
174 1.8 dyoung TAILQ_INSERT_BEFORE(tmp_image, new_image, image_list);
175 1.8 dyoung
176 1.19.2.1 tls new_image->serialno = diskStructure->image_serialno++;
177 1.5 dyoung
178 1.1 fvdl /* TODO : Need to do anything about the boot image in the tree? */
179 1.19.2.1 tls diskStructure->is_bootable = 1;
180 1.5 dyoung
181 1.1 fvdl return 1;
182 1.1 fvdl }
183 1.1 fvdl
184 1.1 fvdl int
185 1.19.2.1 tls cd9660_eltorito_add_boot_option(iso9660_disk *diskStructure,
186 1.19.2.1 tls const char *option_string, const char *value)
187 1.1 fvdl {
188 1.7 dyoung char *eptr;
189 1.1 fvdl struct cd9660_boot_image *image;
190 1.5 dyoung
191 1.1 fvdl assert(option_string != NULL);
192 1.5 dyoung
193 1.1 fvdl /* Find the last image added */
194 1.19.2.1 tls TAILQ_FOREACH(image, &diskStructure->boot_images, image_list) {
195 1.19.2.1 tls if (image->serialno + 1 == diskStructure->image_serialno)
196 1.8 dyoung break;
197 1.8 dyoung }
198 1.1 fvdl if (image == NULL)
199 1.2 dyoung errx(EXIT_FAILURE, "Attempted to add boot option, "
200 1.2 dyoung "but no boot images have been specified");
201 1.1 fvdl
202 1.1 fvdl if (strcmp(option_string, "no-emul-boot") == 0) {
203 1.5 dyoung image->targetMode = ET_MEDIA_NOEM;
204 1.1 fvdl } else if (strcmp(option_string, "no-boot") == 0) {
205 1.1 fvdl image->bootable = ET_NOT_BOOTABLE;
206 1.1 fvdl } else if (strcmp(option_string, "hard-disk-boot") == 0) {
207 1.1 fvdl image->targetMode = ET_MEDIA_HDD;
208 1.1 fvdl } else if (strcmp(option_string, "boot-load-segment") == 0) {
209 1.7 dyoung image->loadSegment = strtoul(value, &eptr, 16);
210 1.7 dyoung if (eptr == value || *eptr != '\0' || errno != ERANGE) {
211 1.7 dyoung warn("%s: strtoul", __func__);
212 1.7 dyoung return 0;
213 1.7 dyoung }
214 1.1 fvdl } else {
215 1.5 dyoung return 0;
216 1.1 fvdl }
217 1.1 fvdl return 1;
218 1.1 fvdl }
219 1.1 fvdl
220 1.1 fvdl static struct boot_catalog_entry *
221 1.1 fvdl cd9660_init_boot_catalog_entry(void)
222 1.1 fvdl {
223 1.19.2.1 tls return ecalloc(1, sizeof(struct boot_catalog_entry));
224 1.1 fvdl }
225 1.1 fvdl
226 1.1 fvdl static struct boot_catalog_entry *
227 1.1 fvdl cd9660_boot_setup_validation_entry(char sys)
228 1.1 fvdl {
229 1.6 dyoung struct boot_catalog_entry *entry;
230 1.6 dyoung boot_catalog_validation_entry *ve;
231 1.1 fvdl int16_t checksum;
232 1.1 fvdl unsigned char *csptr;
233 1.19 christos size_t i;
234 1.6 dyoung entry = cd9660_init_boot_catalog_entry();
235 1.5 dyoung
236 1.6 dyoung ve = &entry->entry_data.VE;
237 1.6 dyoung
238 1.6 dyoung ve->header_id[0] = 1;
239 1.6 dyoung ve->platform_id[0] = sys;
240 1.6 dyoung ve->key[0] = 0x55;
241 1.6 dyoung ve->key[1] = 0xAA;
242 1.1 fvdl
243 1.1 fvdl /* Calculate checksum */
244 1.1 fvdl checksum = 0;
245 1.6 dyoung cd9660_721(0, ve->checksum);
246 1.7 dyoung csptr = (unsigned char*)ve;
247 1.6 dyoung for (i = 0; i < sizeof(*ve); i += 2) {
248 1.6 dyoung checksum += (int16_t)csptr[i];
249 1.6 dyoung checksum += 256 * (int16_t)csptr[i + 1];
250 1.1 fvdl }
251 1.1 fvdl checksum = -checksum;
252 1.6 dyoung cd9660_721(checksum, ve->checksum);
253 1.5 dyoung
254 1.7 dyoung ELTORITO_DPRINTF(("%s: header_id %d, platform_id %d, key[0] %d, key[1] %d, "
255 1.7 dyoung "checksum %04x\n", __func__, ve->header_id[0], ve->platform_id[0],
256 1.7 dyoung ve->key[0], ve->key[1], checksum));
257 1.6 dyoung return entry;
258 1.1 fvdl }
259 1.1 fvdl
260 1.1 fvdl static struct boot_catalog_entry *
261 1.1 fvdl cd9660_boot_setup_default_entry(struct cd9660_boot_image *disk)
262 1.1 fvdl {
263 1.1 fvdl struct boot_catalog_entry *default_entry;
264 1.6 dyoung boot_catalog_initial_entry *ie;
265 1.1 fvdl
266 1.1 fvdl default_entry = cd9660_init_boot_catalog_entry();
267 1.1 fvdl if (default_entry == NULL)
268 1.1 fvdl return NULL;
269 1.5 dyoung
270 1.6 dyoung ie = &default_entry->entry_data.IE;
271 1.5 dyoung
272 1.6 dyoung ie->boot_indicator[0] = disk->bootable;
273 1.6 dyoung ie->media_type[0] = disk->targetMode;
274 1.6 dyoung cd9660_721(disk->loadSegment, ie->load_segment);
275 1.6 dyoung ie->system_type[0] = disk->system;
276 1.7 dyoung cd9660_721(disk->num_sectors, ie->sector_count);
277 1.6 dyoung cd9660_731(disk->sector, ie->load_rba);
278 1.5 dyoung
279 1.7 dyoung ELTORITO_DPRINTF(("%s: boot indicator %d, media type %d, "
280 1.7 dyoung "load segment %04x, system type %d, sector count %d, "
281 1.7 dyoung "load rba %d\n", __func__, ie->boot_indicator[0],
282 1.7 dyoung ie->media_type[0], disk->loadSegment, ie->system_type[0],
283 1.7 dyoung disk->num_sectors, disk->sector));
284 1.1 fvdl return default_entry;
285 1.1 fvdl }
286 1.1 fvdl
287 1.1 fvdl static struct boot_catalog_entry *
288 1.1 fvdl cd9660_boot_setup_section_head(char platform)
289 1.1 fvdl {
290 1.6 dyoung struct boot_catalog_entry *entry;
291 1.6 dyoung boot_catalog_section_header *sh;
292 1.1 fvdl
293 1.6 dyoung entry = cd9660_init_boot_catalog_entry();
294 1.6 dyoung if (entry == NULL)
295 1.1 fvdl return NULL;
296 1.6 dyoung
297 1.6 dyoung sh = &entry->entry_data.SH;
298 1.1 fvdl /* More by default. The last one will manually be set to 0x91 */
299 1.6 dyoung sh->header_indicator[0] = ET_SECTION_HEADER_MORE;
300 1.6 dyoung sh->platform_id[0] = platform;
301 1.6 dyoung sh->num_section_entries[0] = 0;
302 1.6 dyoung return entry;
303 1.1 fvdl }
304 1.1 fvdl
305 1.1 fvdl static struct boot_catalog_entry *
306 1.1 fvdl cd9660_boot_setup_section_entry(struct cd9660_boot_image *disk)
307 1.1 fvdl {
308 1.1 fvdl struct boot_catalog_entry *entry;
309 1.6 dyoung boot_catalog_section_entry *se;
310 1.1 fvdl if ((entry = cd9660_init_boot_catalog_entry()) == NULL)
311 1.1 fvdl return NULL;
312 1.5 dyoung
313 1.6 dyoung se = &entry->entry_data.SE;
314 1.6 dyoung
315 1.6 dyoung se->boot_indicator[0] = ET_BOOTABLE;
316 1.6 dyoung se->media_type[0] = disk->targetMode;
317 1.6 dyoung cd9660_721(disk->loadSegment, se->load_segment);
318 1.7 dyoung cd9660_721(disk->num_sectors, se->sector_count);
319 1.7 dyoung cd9660_731(disk->sector, se->load_rba);
320 1.1 fvdl return entry;
321 1.1 fvdl }
322 1.1 fvdl
323 1.1 fvdl #if 0
324 1.1 fvdl static u_char
325 1.1 fvdl cd9660_boot_get_system_type(struct cd9660_boot_image *disk)
326 1.1 fvdl {
327 1.1 fvdl /*
328 1.1 fvdl For hard drive booting, we need to examine the MBR to figure
329 1.1 fvdl out what the partition type is
330 1.1 fvdl */
331 1.1 fvdl return 0;
332 1.1 fvdl }
333 1.1 fvdl #endif
334 1.1 fvdl
335 1.1 fvdl /*
336 1.1 fvdl * Set up the BVD, Boot catalog, and the boot entries, but do no writing
337 1.1 fvdl */
338 1.1 fvdl int
339 1.19.2.1 tls cd9660_setup_boot(iso9660_disk *diskStructure, int first_sector)
340 1.1 fvdl {
341 1.1 fvdl int sector;
342 1.1 fvdl int used_sectors;
343 1.1 fvdl int num_entries = 0;
344 1.1 fvdl int catalog_sectors;
345 1.1 fvdl struct boot_catalog_entry *x86_head, *mac_head, *ppc_head,
346 1.8 dyoung *valid_entry, *default_entry, *temp, *head, **headp, *next;
347 1.1 fvdl struct cd9660_boot_image *tmp_disk;
348 1.1 fvdl
349 1.8 dyoung headp = NULL;
350 1.8 dyoung x86_head = mac_head = ppc_head = NULL;
351 1.5 dyoung
352 1.1 fvdl /* If there are no boot disks, don't bother building boot information */
353 1.19.2.1 tls if (TAILQ_EMPTY(&diskStructure->boot_images))
354 1.1 fvdl return 0;
355 1.1 fvdl
356 1.1 fvdl /* Point to catalog: For now assume it consumes one sector */
357 1.9 dyoung ELTORITO_DPRINTF(("Boot catalog will go in sector %d\n", first_sector));
358 1.19.2.1 tls diskStructure->boot_catalog_sector = first_sector;
359 1.1 fvdl cd9660_bothendian_dword(first_sector,
360 1.19.2.1 tls diskStructure->boot_descriptor->boot_catalog_pointer);
361 1.5 dyoung
362 1.1 fvdl /* Step 1: Generate boot catalog */
363 1.1 fvdl /* Step 1a: Validation entry */
364 1.1 fvdl valid_entry = cd9660_boot_setup_validation_entry(ET_SYS_X86);
365 1.1 fvdl if (valid_entry == NULL)
366 1.1 fvdl return -1;
367 1.5 dyoung
368 1.1 fvdl /*
369 1.1 fvdl * Count how many boot images there are,
370 1.1 fvdl * and how many sectors they consume.
371 1.1 fvdl */
372 1.1 fvdl num_entries = 1;
373 1.1 fvdl used_sectors = 0;
374 1.5 dyoung
375 1.19.2.1 tls TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) {
376 1.1 fvdl used_sectors += tmp_disk->num_sectors;
377 1.5 dyoung
378 1.1 fvdl /* One default entry per image */
379 1.1 fvdl num_entries++;
380 1.1 fvdl }
381 1.19.2.1 tls catalog_sectors = howmany(num_entries * 0x20, diskStructure->sectorSize);
382 1.1 fvdl used_sectors += catalog_sectors;
383 1.5 dyoung
384 1.19.2.1 tls if (diskStructure->verbose_level > 0) {
385 1.9 dyoung printf("%s: there will be %i entries consuming %i sectors. "
386 1.9 dyoung "Catalog is %i sectors\n", __func__, num_entries,
387 1.9 dyoung used_sectors, catalog_sectors);
388 1.9 dyoung }
389 1.5 dyoung
390 1.1 fvdl /* Populate sector numbers */
391 1.1 fvdl sector = first_sector + catalog_sectors;
392 1.19.2.1 tls TAILQ_FOREACH(tmp_disk, &diskStructure->boot_images, image_list) {
393 1.1 fvdl tmp_disk->sector = sector;
394 1.1 fvdl sector += tmp_disk->num_sectors;
395 1.1 fvdl }
396 1.5 dyoung
397 1.19.2.1 tls LIST_INSERT_HEAD(&diskStructure->boot_entries, valid_entry, ll_struct);
398 1.5 dyoung
399 1.1 fvdl /* Step 1b: Initial/default entry */
400 1.1 fvdl /* TODO : PARAM */
401 1.19.2.1 tls tmp_disk = TAILQ_FIRST(&diskStructure->boot_images);
402 1.1 fvdl default_entry = cd9660_boot_setup_default_entry(tmp_disk);
403 1.1 fvdl if (default_entry == NULL) {
404 1.1 fvdl warnx("Error: memory allocation failed in cd9660_setup_boot");
405 1.1 fvdl return -1;
406 1.1 fvdl }
407 1.5 dyoung
408 1.1 fvdl LIST_INSERT_AFTER(valid_entry, default_entry, ll_struct);
409 1.5 dyoung
410 1.1 fvdl /* Todo: multiple default entries? */
411 1.5 dyoung
412 1.7 dyoung tmp_disk = TAILQ_NEXT(tmp_disk, image_list);
413 1.5 dyoung
414 1.1 fvdl temp = default_entry;
415 1.5 dyoung
416 1.1 fvdl /* If multiple boot images are given : */
417 1.1 fvdl while (tmp_disk != NULL) {
418 1.1 fvdl /* Step 2: Section header */
419 1.1 fvdl switch (tmp_disk->system) {
420 1.1 fvdl case ET_SYS_X86:
421 1.8 dyoung headp = &x86_head;
422 1.1 fvdl break;
423 1.1 fvdl case ET_SYS_PPC:
424 1.8 dyoung headp = &ppc_head;
425 1.1 fvdl break;
426 1.1 fvdl case ET_SYS_MAC:
427 1.8 dyoung headp = &mac_head;
428 1.1 fvdl break;
429 1.8 dyoung default:
430 1.8 dyoung warnx("%s: internal error: unknown system type",
431 1.8 dyoung __func__);
432 1.8 dyoung return -1;
433 1.1 fvdl }
434 1.1 fvdl
435 1.8 dyoung if (*headp == NULL) {
436 1.8 dyoung head =
437 1.5 dyoung cd9660_boot_setup_section_head(tmp_disk->system);
438 1.8 dyoung if (head == NULL) {
439 1.1 fvdl warnx("Error: memory allocation failed in "
440 1.1 fvdl "cd9660_setup_boot");
441 1.1 fvdl return -1;
442 1.1 fvdl }
443 1.8 dyoung LIST_INSERT_AFTER(default_entry, head, ll_struct);
444 1.8 dyoung *headp = head;
445 1.8 dyoung } else
446 1.8 dyoung head = *headp;
447 1.8 dyoung
448 1.8 dyoung head->entry_data.SH.num_section_entries[0]++;
449 1.5 dyoung
450 1.1 fvdl /* Step 2a: Section entry and extensions */
451 1.1 fvdl temp = cd9660_boot_setup_section_entry(tmp_disk);
452 1.1 fvdl if (temp == NULL) {
453 1.8 dyoung warn("%s: cd9660_boot_setup_section_entry", __func__);
454 1.1 fvdl return -1;
455 1.1 fvdl }
456 1.1 fvdl
457 1.8 dyoung while ((next = LIST_NEXT(head, ll_struct)) != NULL &&
458 1.3 dyoung next->entry_type == ET_ENTRY_SE)
459 1.8 dyoung head = next;
460 1.5 dyoung
461 1.8 dyoung LIST_INSERT_AFTER(head, temp, ll_struct);
462 1.7 dyoung tmp_disk = TAILQ_NEXT(tmp_disk, image_list);
463 1.1 fvdl }
464 1.5 dyoung
465 1.1 fvdl /* TODO: Remaining boot disks when implemented */
466 1.5 dyoung
467 1.1 fvdl return first_sector + used_sectors;
468 1.1 fvdl }
469 1.1 fvdl
470 1.1 fvdl int
471 1.19.2.1 tls cd9660_setup_boot_volume_descriptor(iso9660_disk *diskStructure,
472 1.19.2.1 tls volume_descriptor *bvd)
473 1.1 fvdl {
474 1.1 fvdl boot_volume_descriptor *bvdData =
475 1.1 fvdl (boot_volume_descriptor*)bvd->volumeDescriptorData;
476 1.1 fvdl
477 1.1 fvdl bvdData->boot_record_indicator[0] = ISO_VOLUME_DESCRIPTOR_BOOT;
478 1.1 fvdl memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
479 1.1 fvdl bvdData->version[0] = 1;
480 1.1 fvdl memcpy(bvdData->boot_system_identifier, ET_ID, 23);
481 1.1 fvdl memcpy(bvdData->identifier, ISO_VOLUME_DESCRIPTOR_STANDARD_ID, 5);
482 1.19.2.1 tls diskStructure->boot_descriptor =
483 1.1 fvdl (boot_volume_descriptor*) bvd->volumeDescriptorData;
484 1.1 fvdl return 1;
485 1.1 fvdl }
486 1.1 fvdl
487 1.15 christos static int
488 1.15 christos cd9660_write_mbr_partition_entry(FILE *fd, int idx, off_t sector_start,
489 1.15 christos off_t nsectors, int type)
490 1.15 christos {
491 1.15 christos uint8_t val;
492 1.15 christos uint32_t lba;
493 1.15 christos
494 1.15 christos if (fseeko(fd, (off_t)(idx) * 16 + 0x1be, SEEK_SET) == -1)
495 1.15 christos err(1, "fseeko");
496 1.15 christos
497 1.15 christos val = 0x80; /* Bootable */
498 1.15 christos fwrite(&val, sizeof(val), 1, fd);
499 1.15 christos
500 1.15 christos val = 0xff; /* CHS begin */
501 1.15 christos fwrite(&val, sizeof(val), 1, fd);
502 1.15 christos fwrite(&val, sizeof(val), 1, fd);
503 1.15 christos fwrite(&val, sizeof(val), 1, fd);
504 1.15 christos
505 1.15 christos val = type; /* Part type */
506 1.15 christos fwrite(&val, sizeof(val), 1, fd);
507 1.15 christos
508 1.15 christos val = 0xff; /* CHS end */
509 1.15 christos fwrite(&val, sizeof(val), 1, fd);
510 1.15 christos fwrite(&val, sizeof(val), 1, fd);
511 1.15 christos fwrite(&val, sizeof(val), 1, fd);
512 1.15 christos
513 1.15 christos /* LBA extent */
514 1.15 christos lba = htole32(sector_start);
515 1.15 christos fwrite(&lba, sizeof(lba), 1, fd);
516 1.15 christos lba = htole32(nsectors);
517 1.15 christos fwrite(&lba, sizeof(lba), 1, fd);
518 1.15 christos
519 1.15 christos return 0;
520 1.15 christos }
521 1.15 christos
522 1.15 christos static int
523 1.15 christos cd9660_write_apm_partition_entry(FILE *fd, int idx, int total_partitions,
524 1.15 christos off_t sector_start, off_t nsectors, off_t sector_size,
525 1.15 christos const char *part_name, const char *part_type)
526 1.15 christos {
527 1.18 christos uint32_t apm32, part_status;
528 1.15 christos uint16_t apm16;
529 1.15 christos
530 1.18 christos /* See Apple Tech Note 1189 for the details about the pmPartStatus
531 1.18 christos * flags.
532 1.18 christos * Below the flags which are default:
533 1.18 christos * - IsValid 0x01
534 1.18 christos * - IsAllocated 0x02
535 1.18 christos * - IsReadable 0x10
536 1.18 christos * - IsWritable 0x20
537 1.18 christos */
538 1.18 christos part_status = APPLE_PS_VALID | APPLE_PS_ALLOCATED | APPLE_PS_READABLE |
539 1.18 christos APPLE_PS_WRITABLE;
540 1.18 christos
541 1.15 christos if (fseeko(fd, (off_t)(idx + 1) * sector_size, SEEK_SET) == -1)
542 1.15 christos err(1, "fseeko");
543 1.15 christos
544 1.15 christos /* Signature */
545 1.15 christos apm16 = htobe16(0x504d);
546 1.15 christos fwrite(&apm16, sizeof(apm16), 1, fd);
547 1.15 christos apm16 = 0;
548 1.15 christos fwrite(&apm16, sizeof(apm16), 1, fd);
549 1.15 christos
550 1.15 christos /* Total number of partitions */
551 1.15 christos apm32 = htobe32(total_partitions);
552 1.15 christos fwrite(&apm32, sizeof(apm32), 1, fd);
553 1.15 christos /* Bounds */
554 1.15 christos apm32 = htobe32(sector_start);
555 1.15 christos fwrite(&apm32, sizeof(apm32), 1, fd);
556 1.15 christos apm32 = htobe32(nsectors);
557 1.15 christos fwrite(&apm32, sizeof(apm32), 1, fd);
558 1.15 christos
559 1.15 christos fwrite(part_name, strlen(part_name) + 1, 1, fd);
560 1.15 christos fseek(fd, 32 - strlen(part_name) - 1, SEEK_CUR);
561 1.15 christos fwrite(part_type, strlen(part_type) + 1, 1, fd);
562 1.18 christos fseek(fd, 32 - strlen(part_type) - 1, SEEK_CUR);
563 1.18 christos
564 1.18 christos apm32 = 0;
565 1.18 christos /* pmLgDataStart */
566 1.18 christos fwrite(&apm32, sizeof(apm32), 1, fd);
567 1.18 christos /* pmDataCnt */
568 1.18 christos apm32 = htobe32(nsectors);
569 1.18 christos fwrite(&apm32, sizeof(apm32), 1, fd);
570 1.18 christos /* pmPartStatus */
571 1.18 christos apm32 = htobe32(part_status);
572 1.18 christos fwrite(&apm32, sizeof(apm32), 1, fd);
573 1.15 christos
574 1.15 christos return 0;
575 1.15 christos }
576 1.15 christos
577 1.5 dyoung int
578 1.19.2.1 tls cd9660_write_boot(iso9660_disk *diskStructure, FILE *fd)
579 1.1 fvdl {
580 1.1 fvdl struct boot_catalog_entry *e;
581 1.1 fvdl struct cd9660_boot_image *t;
582 1.15 christos int apm_partitions = 0;
583 1.15 christos int mbr_partitions = 0;
584 1.1 fvdl
585 1.1 fvdl /* write boot catalog */
586 1.19.2.1 tls if (fseeko(fd, (off_t)diskStructure->boot_catalog_sector *
587 1.19.2.1 tls diskStructure->sectorSize, SEEK_SET) == -1)
588 1.13 christos err(1, "fseeko");
589 1.5 dyoung
590 1.19.2.1 tls if (diskStructure->verbose_level > 0) {
591 1.14 christos printf("Writing boot catalog to sector %" PRId64 "\n",
592 1.19.2.1 tls diskStructure->boot_catalog_sector);
593 1.9 dyoung }
594 1.19.2.1 tls LIST_FOREACH(e, &diskStructure->boot_entries, ll_struct) {
595 1.19.2.1 tls if (diskStructure->verbose_level > 0) {
596 1.9 dyoung printf("Writing catalog entry of type %d\n",
597 1.9 dyoung e->entry_type);
598 1.9 dyoung }
599 1.1 fvdl /*
600 1.1 fvdl * It doesnt matter which one gets written
601 1.1 fvdl * since they are the same size
602 1.1 fvdl */
603 1.1 fvdl fwrite(&(e->entry_data.VE), 1, 32, fd);
604 1.1 fvdl }
605 1.19.2.1 tls if (diskStructure->verbose_level > 0)
606 1.10 dyoung printf("Finished writing boot catalog\n");
607 1.5 dyoung
608 1.1 fvdl /* copy boot images */
609 1.19.2.1 tls TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
610 1.19.2.1 tls if (diskStructure->verbose_level > 0) {
611 1.9 dyoung printf("Writing boot image from %s to sectors %d\n",
612 1.9 dyoung t->filename, t->sector);
613 1.9 dyoung }
614 1.19.2.1 tls cd9660_copy_file(diskStructure, fd, t->sector, t->filename);
615 1.15 christos
616 1.15 christos if (t->system == ET_SYS_MAC)
617 1.15 christos apm_partitions++;
618 1.15 christos if (t->system == ET_SYS_PPC)
619 1.15 christos mbr_partitions++;
620 1.15 christos }
621 1.15 christos
622 1.15 christos /* some systems need partition tables as well */
623 1.19.2.1 tls if (mbr_partitions > 0 || diskStructure->chrp_boot) {
624 1.15 christos uint16_t sig;
625 1.15 christos
626 1.15 christos fseek(fd, 0x1fe, SEEK_SET);
627 1.15 christos sig = htole16(0xaa55);
628 1.15 christos fwrite(&sig, sizeof(sig), 1, fd);
629 1.15 christos
630 1.15 christos mbr_partitions = 0;
631 1.15 christos
632 1.15 christos /* Write ISO9660 descriptor, enclosing the whole disk */
633 1.19.2.1 tls if (diskStructure->chrp_boot)
634 1.15 christos cd9660_write_mbr_partition_entry(fd, mbr_partitions++,
635 1.19.2.1 tls 0, diskStructure->totalSectors *
636 1.19.2.1 tls (diskStructure->sectorSize / 512), 0x96);
637 1.15 christos
638 1.15 christos /* Write all partition entries */
639 1.19.2.1 tls TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
640 1.15 christos if (t->system != ET_SYS_PPC)
641 1.15 christos continue;
642 1.15 christos cd9660_write_mbr_partition_entry(fd, mbr_partitions++,
643 1.19.2.1 tls t->sector * (diskStructure->sectorSize / 512),
644 1.19.2.1 tls t->num_sectors * (diskStructure->sectorSize / 512),
645 1.15 christos 0x41 /* PReP Boot */);
646 1.15 christos }
647 1.15 christos }
648 1.15 christos
649 1.15 christos if (apm_partitions > 0) {
650 1.15 christos /* Write DDR and global APM info */
651 1.15 christos uint32_t apm32;
652 1.15 christos uint16_t apm16;
653 1.15 christos int total_parts;
654 1.15 christos
655 1.15 christos fseek(fd, 0, SEEK_SET);
656 1.15 christos apm16 = htobe16(0x4552);
657 1.15 christos fwrite(&apm16, sizeof(apm16), 1, fd);
658 1.15 christos /* Device block size */
659 1.15 christos apm16 = htobe16(512);
660 1.15 christos fwrite(&apm16, sizeof(apm16), 1, fd);
661 1.15 christos /* Device block count */
662 1.19.2.1 tls apm32 = htobe32(diskStructure->totalSectors *
663 1.19.2.1 tls (diskStructure->sectorSize / 512));
664 1.15 christos fwrite(&apm32, sizeof(apm32), 1, fd);
665 1.15 christos /* Device type/id */
666 1.15 christos apm16 = htobe16(1);
667 1.15 christos fwrite(&apm16, sizeof(apm16), 1, fd);
668 1.15 christos fwrite(&apm16, sizeof(apm16), 1, fd);
669 1.15 christos
670 1.15 christos /* Count total needed entries */
671 1.15 christos total_parts = 2 + apm_partitions; /* Self + ISO9660 */
672 1.15 christos
673 1.15 christos /* Write self-descriptor */
674 1.15 christos cd9660_write_apm_partition_entry(fd, 0, total_parts, 1,
675 1.15 christos total_parts, 512, "Apple", "Apple_partition_map");
676 1.15 christos
677 1.15 christos /* Write all partition entries */
678 1.15 christos apm_partitions = 0;
679 1.19.2.1 tls TAILQ_FOREACH(t, &diskStructure->boot_images, image_list) {
680 1.15 christos if (t->system != ET_SYS_MAC)
681 1.15 christos continue;
682 1.15 christos
683 1.15 christos cd9660_write_apm_partition_entry(fd,
684 1.18 christos 1 + apm_partitions++, total_parts,
685 1.19.2.1 tls t->sector * (diskStructure->sectorSize / 512),
686 1.19.2.1 tls t->num_sectors * (diskStructure->sectorSize / 512),
687 1.15 christos 512, "CD Boot", "Apple_Bootstrap");
688 1.15 christos }
689 1.18 christos
690 1.18 christos /* Write ISO9660 descriptor, enclosing the whole disk */
691 1.18 christos cd9660_write_apm_partition_entry(fd, 2 + apm_partitions,
692 1.19.2.1 tls total_parts, 0, diskStructure->totalSectors *
693 1.19.2.1 tls (diskStructure->sectorSize / 512), 512, "ISO9660",
694 1.18 christos "CD_ROM_Mode_1");
695 1.1 fvdl }
696 1.1 fvdl
697 1.1 fvdl return 0;
698 1.1 fvdl }
699