| 1 |
blade |
2 |
/* @(#)eltorito.c 1.33 05/02/27 joerg */
|
| 2 |
|
|
#ifndef lint
|
| 3 |
|
|
static char sccsid[] =
|
| 4 |
|
|
"@(#)eltorito.c 1.33 05/02/27 joerg";
|
| 5 |
|
|
|
| 6 |
|
|
#endif
|
| 7 |
|
|
/*
|
| 8 |
|
|
* Program eltorito.c - Handle El Torito specific extensions to iso9660.
|
| 9 |
|
|
*
|
| 10 |
|
|
*
|
| 11 |
|
|
* Written by Michael Fulbright <msf@redhat.com> (1996).
|
| 12 |
|
|
*
|
| 13 |
|
|
* Copyright 1996 RedHat Software, Incorporated
|
| 14 |
|
|
* Copyright (c) 1999-2004 J. Schilling
|
| 15 |
|
|
*
|
| 16 |
|
|
* This program is free software; you can redistribute it and/or modify
|
| 17 |
|
|
* it under the terms of the GNU General Public License as published by
|
| 18 |
|
|
* the Free Software Foundation; either version 2, or (at your option)
|
| 19 |
|
|
* any later version.
|
| 20 |
|
|
*
|
| 21 |
|
|
* This program is distributed in the hope that it will be useful,
|
| 22 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 23 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 24 |
|
|
* GNU General Public License for more details.
|
| 25 |
|
|
*
|
| 26 |
|
|
* You should have received a copy of the GNU General Public License
|
| 27 |
|
|
* along with this program; if not, write to the Free Software
|
| 28 |
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
| 29 |
|
|
*/
|
| 30 |
|
|
|
| 31 |
|
|
#include <mconfig.h>
|
| 32 |
|
|
#include "mkisofs.h"
|
| 33 |
|
|
#include <fctldefs.h>
|
| 34 |
|
|
#include <utypes.h>
|
| 35 |
|
|
#include <intcvt.h>
|
| 36 |
|
|
#include "match.h"
|
| 37 |
|
|
#include "diskmbr.h"
|
| 38 |
|
|
#include "bootinfo.h"
|
| 39 |
|
|
#include <schily.h>
|
| 40 |
|
|
|
| 41 |
|
|
#undef MIN
|
| 42 |
|
|
#define MIN(a, b) (((a) < (b))? (a): (b))
|
| 43 |
|
|
|
| 44 |
|
|
static struct eltorito_validation_entry valid_desc;
|
| 45 |
|
|
static struct eltorito_boot_descriptor gboot_desc;
|
| 46 |
|
|
static struct disk_master_boot_record disk_mbr;
|
| 47 |
|
|
static unsigned int bcat_de_flags;
|
| 48 |
|
|
|
| 49 |
|
|
void init_boot_catalog __PR((const char *path));
|
| 50 |
|
|
void insert_boot_cat __PR((void));
|
| 51 |
|
|
static void get_torito_desc __PR((struct eltorito_boot_descriptor *boot_desc));
|
| 52 |
|
|
static void fill_boot_desc __PR((struct eltorito_defaultboot_entry *boot_desc_entry,
|
| 53 |
|
|
struct eltorito_boot_entry_info *boot_entry));
|
| 54 |
|
|
void get_boot_entry __PR((void));
|
| 55 |
|
|
void new_boot_entry __PR((void));
|
| 56 |
|
|
static int tvd_write __PR((FILE * outfile));
|
| 57 |
|
|
|
| 58 |
|
|
|
| 59 |
|
|
LOCAL char *bootcat_path; /* filename of boot catalog */
|
| 60 |
|
|
/*
|
| 61 |
|
|
* Make sure any existing boot catalog is excluded
|
| 62 |
|
|
*/
|
| 63 |
|
|
void
|
| 64 |
|
|
init_boot_catalog(path)
|
| 65 |
|
|
const char *path;
|
| 66 |
|
|
{
|
| 67 |
|
|
#ifdef SORTING
|
| 68 |
|
|
struct eltorito_boot_entry_info * cbe;
|
| 69 |
|
|
|
| 70 |
|
|
for (cbe = first_boot_entry;
|
| 71 |
|
|
cbe != NULL;
|
| 72 |
|
|
cbe = cbe->next) {
|
| 73 |
|
|
char *p;
|
| 74 |
|
|
|
| 75 |
|
|
if (cbe->boot_image == NULL)
|
| 76 |
|
|
comerrno(EX_BAD, "Missing boot image name, use -eltorito-boot option.\n");
|
| 77 |
|
|
p = (char *) e_malloc(strlen(cbe->boot_image) + strlen(path) + 2);
|
| 78 |
|
|
strcpy(p, path);
|
| 79 |
|
|
if (p[strlen(p) - 1] != '/') {
|
| 80 |
|
|
strcat(p, "/");
|
| 81 |
|
|
}
|
| 82 |
|
|
strcat(p, cbe->boot_image);
|
| 83 |
|
|
add_sort_match(p, sort_matches(p, 1));
|
| 84 |
|
|
free(p);
|
| 85 |
|
|
}
|
| 86 |
|
|
#endif
|
| 87 |
|
|
bootcat_path = (char *) e_malloc(strlen(boot_catalog) + strlen(path) + 2);
|
| 88 |
|
|
strcpy(bootcat_path, path);
|
| 89 |
|
|
if (bootcat_path[strlen(bootcat_path) - 1] != '/') {
|
| 90 |
|
|
strcat(bootcat_path, "/");
|
| 91 |
|
|
}
|
| 92 |
|
|
strcat(bootcat_path, boot_catalog);
|
| 93 |
|
|
|
| 94 |
|
|
/*
|
| 95 |
|
|
* we are going to create a virtual catalog file
|
| 96 |
|
|
* - so make sure any existing is excluded
|
| 97 |
|
|
*/
|
| 98 |
|
|
add_match(bootcat_path);
|
| 99 |
|
|
|
| 100 |
|
|
/* flag the file as a memory file */
|
| 101 |
|
|
bcat_de_flags = MEMORY_FILE;
|
| 102 |
|
|
|
| 103 |
|
|
/* find out if we want to "hide" this file */
|
| 104 |
|
|
if (i_matches(boot_catalog) || i_matches(bootcat_path))
|
| 105 |
|
|
bcat_de_flags |= INHIBIT_ISO9660_ENTRY;
|
| 106 |
|
|
|
| 107 |
|
|
if (j_matches(boot_catalog) || j_matches(bootcat_path))
|
| 108 |
|
|
bcat_de_flags |= INHIBIT_JOLIET_ENTRY;
|
| 109 |
|
|
|
| 110 |
|
|
}/* init_boot_catalog(... */
|
| 111 |
|
|
|
| 112 |
|
|
/*
|
| 113 |
|
|
* Create a boot catalog file in memory - mkisofs already uses this type of
|
| 114 |
|
|
* file for the TRANS.TBL files. Therefore the boot catalog is set up in
|
| 115 |
|
|
* similar way
|
| 116 |
|
|
*/
|
| 117 |
|
|
void
|
| 118 |
|
|
insert_boot_cat()
|
| 119 |
|
|
{
|
| 120 |
|
|
struct directory_entry *de;
|
| 121 |
|
|
struct directory_entry *s_entry;
|
| 122 |
|
|
char *p1;
|
| 123 |
|
|
char *p2;
|
| 124 |
|
|
char *p3;
|
| 125 |
|
|
struct directory *this_dir;
|
| 126 |
|
|
struct directory *dir;
|
| 127 |
|
|
char *buffer;
|
| 128 |
|
|
|
| 129 |
|
|
init_fstatbuf();
|
| 130 |
|
|
|
| 131 |
|
|
buffer = (char *) e_malloc(SECTOR_SIZE);
|
| 132 |
|
|
memset(buffer, 0, SECTOR_SIZE);
|
| 133 |
|
|
|
| 134 |
|
|
/*
|
| 135 |
|
|
* try to find the directory that will contain the boot.cat file
|
| 136 |
|
|
* - not very neat, but I can't think of a better way
|
| 137 |
|
|
*/
|
| 138 |
|
|
p1 = strdup(boot_catalog);
|
| 139 |
|
|
|
| 140 |
|
|
/* get dirname (p1) and basename (p2) of boot.cat */
|
| 141 |
|
|
if ((p2 = strrchr(p1, '/')) != NULL) {
|
| 142 |
|
|
*p2 = '\0';
|
| 143 |
|
|
p2++;
|
| 144 |
|
|
|
| 145 |
|
|
/* find the dirname directory entry */
|
| 146 |
|
|
de = search_tree_file(root, p1);
|
| 147 |
|
|
if (!de) {
|
| 148 |
|
|
#ifdef USE_LIBSCHILY
|
| 149 |
|
|
comerrno(EX_BAD,
|
| 150 |
|
|
"Uh oh, I cant find the boot catalog directory '%s'!\n",
|
| 151 |
|
|
p1);
|
| 152 |
|
|
#else
|
| 153 |
|
|
fprintf(stderr,
|
| 154 |
|
|
"Uh oh, I cant find the boot catalog directory '%s'!\n",
|
| 155 |
|
|
p1);
|
| 156 |
|
|
exit(1);
|
| 157 |
|
|
#endif
|
| 158 |
|
|
}
|
| 159 |
|
|
this_dir = 0;
|
| 160 |
|
|
|
| 161 |
|
|
/* get the basename (p3) of the directory */
|
| 162 |
|
|
if ((p3 = strrchr(p1, '/')) != NULL)
|
| 163 |
|
|
p3++;
|
| 164 |
|
|
else
|
| 165 |
|
|
p3 = p1;
|
| 166 |
|
|
|
| 167 |
|
|
/* find the correct sub-directory entry */
|
| 168 |
|
|
for (dir = de->filedir->subdir; dir; dir = dir->next)
|
| 169 |
|
|
if (!(strcmp(dir->de_name, p3)))
|
| 170 |
|
|
this_dir = dir;
|
| 171 |
|
|
|
| 172 |
|
|
if (this_dir == 0) {
|
| 173 |
|
|
#ifdef USE_LIBSCHILY
|
| 174 |
|
|
comerrno(EX_BAD,
|
| 175 |
|
|
"Uh oh, I cant find the boot catalog directory '%s'!\n",
|
| 176 |
|
|
p3);
|
| 177 |
|
|
#else
|
| 178 |
|
|
fprintf(stderr,
|
| 179 |
|
|
"Uh oh, I cant find the boot catalog directory '%s'!\n",
|
| 180 |
|
|
p3);
|
| 181 |
|
|
exit(1);
|
| 182 |
|
|
#endif
|
| 183 |
|
|
}
|
| 184 |
|
|
} else {
|
| 185 |
|
|
/* boot.cat is in the root directory */
|
| 186 |
|
|
this_dir = root;
|
| 187 |
|
|
p2 = p1;
|
| 188 |
|
|
}
|
| 189 |
|
|
|
| 190 |
|
|
/*
|
| 191 |
|
|
* make a directory entry in memory (using the same set up as for table
|
| 192 |
|
|
* entries
|
| 193 |
|
|
*/
|
| 194 |
|
|
s_entry = (struct directory_entry *)
|
| 195 |
|
|
e_malloc(sizeof (struct directory_entry));
|
| 196 |
|
|
memset(s_entry, 0, sizeof (struct directory_entry));
|
| 197 |
|
|
s_entry->next = this_dir->contents;
|
| 198 |
|
|
this_dir->contents = s_entry;
|
| 199 |
|
|
|
| 200 |
|
|
#ifdef SORTING
|
| 201 |
|
|
/* inherit any sort weight from parent directory */
|
| 202 |
|
|
s_entry->sort = this_dir->sort;
|
| 203 |
|
|
s_entry->sort += 2;
|
| 204 |
|
|
|
| 205 |
|
|
/* see if this entry should have a new weighting */
|
| 206 |
|
|
if (do_sort) {
|
| 207 |
|
|
s_entry->sort = sort_matches(bootcat_path, s_entry->sort);
|
| 208 |
|
|
}
|
| 209 |
|
|
#endif /* SORTING */
|
| 210 |
|
|
|
| 211 |
|
|
s_entry->isorec.flags[0] = ISO_FILE;
|
| 212 |
|
|
s_entry->priority = 32768;
|
| 213 |
|
|
iso9660_date(s_entry->isorec.date, fstatbuf.st_mtime);
|
| 214 |
|
|
s_entry->inode = TABLE_INODE;
|
| 215 |
|
|
s_entry->dev = (dev_t) UNCACHED_DEVICE;
|
| 216 |
|
|
set_723(s_entry->isorec.volume_sequence_number,
|
| 217 |
|
|
volume_sequence_number);
|
| 218 |
|
|
set_733((char *) s_entry->isorec.size, SECTOR_SIZE);
|
| 219 |
|
|
s_entry->size = SECTOR_SIZE;
|
| 220 |
|
|
s_entry->filedir = this_dir;
|
| 221 |
|
|
s_entry->name = strdup(p2);
|
| 222 |
|
|
iso9660_file_length(p2, s_entry, 0);
|
| 223 |
|
|
|
| 224 |
|
|
/* flag file as necessary */
|
| 225 |
|
|
s_entry->de_flags = bcat_de_flags;
|
| 226 |
|
|
|
| 227 |
|
|
if ((use_XA || use_RockRidge) &&
|
| 228 |
|
|
!(bcat_de_flags & INHIBIT_ISO9660_ENTRY)) {
|
| 229 |
|
|
fstatbuf.st_mode = 0444 | S_IFREG;
|
| 230 |
|
|
fstatbuf.st_nlink = 1;
|
| 231 |
|
|
generate_xa_rr_attributes("",
|
| 232 |
|
|
p2, s_entry,
|
| 233 |
|
|
&fstatbuf, &fstatbuf, 0);
|
| 234 |
|
|
}
|
| 235 |
|
|
/*
|
| 236 |
|
|
* memory files are stored at s_entry->table
|
| 237 |
|
|
* - but this is also used for each s_entry to generate
|
| 238 |
|
|
* TRANS.TBL entries. So if we are generating tables,
|
| 239 |
|
|
* store the TRANS.TBL data here for the moment
|
| 240 |
|
|
*/
|
| 241 |
|
|
if (generate_tables && !(bcat_de_flags & INHIBIT_ISO9660_ENTRY)) {
|
| 242 |
|
|
sprintf(buffer, "F\t%s\n", s_entry->name);
|
| 243 |
|
|
|
| 244 |
|
|
/* copy the TRANS.TBL entry info and clear the buffer */
|
| 245 |
|
|
s_entry->table = strdup(buffer);
|
| 246 |
|
|
memset(buffer, 0, SECTOR_SIZE);
|
| 247 |
|
|
|
| 248 |
|
|
/*
|
| 249 |
|
|
* store the (empty) file data in the
|
| 250 |
|
|
* unused s_entry->whole_name element for the time being
|
| 251 |
|
|
* - this will be transferred to s_entry->table after any
|
| 252 |
|
|
* TRANS.TBL processing later
|
| 253 |
|
|
*/
|
| 254 |
|
|
s_entry->whole_name = buffer;
|
| 255 |
|
|
} else {
|
| 256 |
|
|
/* store the (empty) file data in the s_entry->table element */
|
| 257 |
|
|
s_entry->table = buffer;
|
| 258 |
|
|
s_entry->whole_name = NULL;
|
| 259 |
|
|
}
|
| 260 |
|
|
}
|
| 261 |
|
|
|
| 262 |
|
|
static void
|
| 263 |
|
|
get_torito_desc(boot_desc)
|
| 264 |
|
|
struct eltorito_boot_descriptor *boot_desc;
|
| 265 |
|
|
{
|
| 266 |
|
|
int checksum;
|
| 267 |
|
|
unsigned char *checksum_ptr;
|
| 268 |
|
|
struct directory_entry *de2; /* Boot catalog */
|
| 269 |
|
|
int i;
|
| 270 |
|
|
int offset;
|
| 271 |
|
|
struct eltorito_defaultboot_entry boot_desc_record;
|
| 272 |
|
|
|
| 273 |
|
|
memset(boot_desc, 0, sizeof (*boot_desc));
|
| 274 |
|
|
boot_desc->type[0] = 0;
|
| 275 |
|
|
memcpy(boot_desc->id, ISO_STANDARD_ID, sizeof (ISO_STANDARD_ID));
|
| 276 |
|
|
boot_desc->version[0] = 1;
|
| 277 |
|
|
|
| 278 |
|
|
memcpy(boot_desc->system_id, EL_TORITO_ID, sizeof (EL_TORITO_ID));
|
| 279 |
|
|
|
| 280 |
|
|
/*
|
| 281 |
|
|
* search from root of iso fs to find boot catalog
|
| 282 |
|
|
* - we already know where the boot catalog is
|
| 283 |
|
|
* - we created it above - but lets search for it anyway
|
| 284 |
|
|
* - good sanity check!
|
| 285 |
|
|
*/
|
| 286 |
|
|
de2 = search_tree_file(root, boot_catalog);
|
| 287 |
|
|
if (!de2 || !(de2->de_flags & MEMORY_FILE)) {
|
| 288 |
|
|
#ifdef USE_LIBSCHILY
|
| 289 |
|
|
comerrno(EX_BAD, "Uh oh, I cant find the boot catalog '%s'!\n",
|
| 290 |
|
|
boot_catalog);
|
| 291 |
|
|
#else
|
| 292 |
|
|
fprintf(stderr, "Uh oh, I cant find the boot catalog '%s'!\n",
|
| 293 |
|
|
boot_catalog);
|
| 294 |
|
|
exit(1);
|
| 295 |
|
|
#endif
|
| 296 |
|
|
}
|
| 297 |
|
|
set_731(boot_desc->bootcat_ptr,
|
| 298 |
|
|
(unsigned int) get_733(de2->isorec.extent));
|
| 299 |
|
|
|
| 300 |
|
|
/*
|
| 301 |
|
|
* we have the boot image, so write boot catalog information
|
| 302 |
|
|
* Next we write out the primary descriptor for the disc
|
| 303 |
|
|
*/
|
| 304 |
|
|
memset(&valid_desc, 0, sizeof (valid_desc));
|
| 305 |
|
|
valid_desc.headerid[0] = 1;
|
| 306 |
|
|
valid_desc.arch[0] = EL_TORITO_ARCH_x86;
|
| 307 |
|
|
|
| 308 |
|
|
/*
|
| 309 |
|
|
* we'll shove start of publisher id into id field,
|
| 310 |
|
|
* may get truncated but who really reads this stuff!
|
| 311 |
|
|
*/
|
| 312 |
|
|
if (publisher)
|
| 313 |
|
|
memcpy_max(valid_desc.id, publisher,
|
| 314 |
|
|
MIN(23, strlen(publisher)));
|
| 315 |
|
|
|
| 316 |
|
|
valid_desc.key1[0] = (char) 0x55;
|
| 317 |
|
|
valid_desc.key2[0] = (char) 0xAA;
|
| 318 |
|
|
|
| 319 |
|
|
/* compute the checksum */
|
| 320 |
|
|
checksum = 0;
|
| 321 |
|
|
checksum_ptr = (unsigned char *) &valid_desc;
|
| 322 |
|
|
/* Set checksum to 0 before computing checksum */
|
| 323 |
|
|
set_721(valid_desc.cksum, 0);
|
| 324 |
|
|
for (i = 0; i < (int)sizeof (valid_desc); i += 2) {
|
| 325 |
|
|
checksum += (unsigned int) checksum_ptr[i];
|
| 326 |
|
|
checksum += ((unsigned int) checksum_ptr[i + 1]) * 256;
|
| 327 |
|
|
}
|
| 328 |
|
|
|
| 329 |
|
|
/* now find out the real checksum */
|
| 330 |
|
|
checksum = -checksum;
|
| 331 |
|
|
set_721(valid_desc.cksum, (unsigned int) checksum);
|
| 332 |
|
|
|
| 333 |
|
|
/* now write it to the virtual boot catalog */
|
| 334 |
|
|
memcpy(de2->table, &valid_desc, 32);
|
| 335 |
|
|
|
| 336 |
|
|
for (current_boot_entry = first_boot_entry, offset = sizeof (valid_desc);
|
| 337 |
|
|
current_boot_entry != NULL;
|
| 338 |
|
|
current_boot_entry = current_boot_entry->next,
|
| 339 |
|
|
offset += sizeof (boot_desc_record)) {
|
| 340 |
|
|
|
| 341 |
|
|
if (offset >= SECTOR_SIZE) {
|
| 342 |
|
|
#ifdef USE_LIBSCHILY
|
| 343 |
|
|
comerrno(EX_BAD,
|
| 344 |
|
|
"Too many El Torito boot entries\n");
|
| 345 |
|
|
#else
|
| 346 |
|
|
fprintf(stderr,
|
| 347 |
|
|
"Too many El Torito boot entries\n");
|
| 348 |
|
|
exit(1);
|
| 349 |
|
|
#endif
|
| 350 |
|
|
}
|
| 351 |
|
|
fill_boot_desc(&boot_desc_record, current_boot_entry);
|
| 352 |
|
|
memcpy(de2->table + offset, &boot_desc_record,
|
| 353 |
|
|
sizeof (boot_desc_record));
|
| 354 |
|
|
}
|
| 355 |
|
|
}/* get_torito_desc(... */
|
| 356 |
|
|
|
| 357 |
|
|
static void
|
| 358 |
|
|
fill_boot_desc(boot_desc_entry, boot_entry)
|
| 359 |
|
|
struct eltorito_defaultboot_entry *boot_desc_entry;
|
| 360 |
|
|
struct eltorito_boot_entry_info *boot_entry;
|
| 361 |
|
|
{
|
| 362 |
|
|
struct directory_entry *de; /* Boot file */
|
| 363 |
|
|
int bootmbr;
|
| 364 |
|
|
int i;
|
| 365 |
|
|
int nsectors;
|
| 366 |
|
|
int geosec;
|
| 367 |
|
|
|
| 368 |
|
|
if (!boot_desc_entry || !boot_entry)
|
| 369 |
|
|
return;
|
| 370 |
|
|
|
| 371 |
|
|
/* now adjust boot catalog lets find boot image first */
|
| 372 |
|
|
de = search_tree_file(root, boot_entry->boot_image);
|
| 373 |
|
|
if (!de) {
|
| 374 |
|
|
#ifdef USE_LIBSCHILY
|
| 375 |
|
|
comerrno(EX_BAD, "Uh oh, I cant find the boot image '%s' !\n",
|
| 376 |
|
|
boot_entry->boot_image);
|
| 377 |
|
|
#else
|
| 378 |
|
|
fprintf(stderr, "Uh oh, I cant find the boot image '%s' !\n",
|
| 379 |
|
|
boot_entry->boot_image);
|
| 380 |
|
|
exit(1);
|
| 381 |
|
|
#endif
|
| 382 |
|
|
}
|
| 383 |
|
|
/* now make the initial/default entry for boot catalog */
|
| 384 |
|
|
memset(boot_desc_entry, 0, sizeof (*boot_desc_entry));
|
| 385 |
|
|
boot_desc_entry->boot_id[0] = (char) boot_entry->not_bootable ?
|
| 386 |
|
|
EL_TORITO_NOT_BOOTABLE : EL_TORITO_BOOTABLE;
|
| 387 |
|
|
|
| 388 |
|
|
/* use default BIOS loadpnt */
|
| 389 |
|
|
set_721(boot_desc_entry->loadseg, boot_entry->load_addr);
|
| 390 |
|
|
|
| 391 |
|
|
/*
|
| 392 |
|
|
* figure out size of boot image in 512-byte sectors.
|
| 393 |
|
|
* However, round up to the nearest integral CD (2048-byte) sector.
|
| 394 |
|
|
* This is only used for no-emulation booting.
|
| 395 |
|
|
*/
|
| 396 |
|
|
nsectors = boot_entry->load_size ? boot_entry->load_size :
|
| 397 |
|
|
ISO_BLOCKS(de->size) * (SECTOR_SIZE/512);
|
| 398 |
|
|
|
| 399 |
|
|
if (verbose > 0) {
|
| 400 |
|
|
fprintf(stderr,
|
| 401 |
|
|
"Size of boot image is %d sectors -> ", nsectors);
|
| 402 |
|
|
}
|
| 403 |
|
|
|
| 404 |
|
|
if (boot_entry->hard_disk_boot) {
|
| 405 |
|
|
/* sanity test hard disk boot image */
|
| 406 |
|
|
boot_desc_entry->boot_media[0] = EL_TORITO_MEDIA_HD;
|
| 407 |
|
|
if (verbose > 0)
|
| 408 |
|
|
fprintf(stderr, "Emulating a hard disk\n");
|
| 409 |
|
|
|
| 410 |
|
|
/* read MBR */
|
| 411 |
|
|
bootmbr = open(de->whole_name, O_RDONLY | O_BINARY);
|
| 412 |
|
|
if (bootmbr == -1) {
|
| 413 |
|
|
#ifdef USE_LIBSCHILY
|
| 414 |
|
|
comerr("Error opening boot image '%s' for read.\n",
|
| 415 |
|
|
de->whole_name);
|
| 416 |
|
|
#else
|
| 417 |
|
|
fprintf(stderr,
|
| 418 |
|
|
"Error opening boot image '%s' for read.\n",
|
| 419 |
|
|
de->whole_name);
|
| 420 |
|
|
perror("");
|
| 421 |
|
|
exit(1);
|
| 422 |
|
|
#endif
|
| 423 |
|
|
}
|
| 424 |
|
|
if (read(bootmbr, &disk_mbr, sizeof (disk_mbr)) !=
|
| 425 |
|
|
sizeof (disk_mbr)) {
|
| 426 |
|
|
#ifdef USE_LIBSCHILY
|
| 427 |
|
|
comerr("Error reading MBR from boot image '%s'.\n",
|
| 428 |
|
|
de->whole_name);
|
| 429 |
|
|
#else
|
| 430 |
|
|
fprintf(stderr,
|
| 431 |
|
|
"Error reading MBR from boot image '%s'.\n",
|
| 432 |
|
|
de->whole_name);
|
| 433 |
|
|
exit(1);
|
| 434 |
|
|
#endif
|
| 435 |
|
|
}
|
| 436 |
|
|
close(bootmbr);
|
| 437 |
|
|
if (la_to_u_2_byte(disk_mbr.magic) != MBR_MAGIC) {
|
| 438 |
|
|
#ifdef USE_LIBSCHILY
|
| 439 |
|
|
errmsgno(EX_BAD,
|
| 440 |
|
|
"Warning: boot image '%s' MBR is not a boot sector.\n",
|
| 441 |
|
|
de->whole_name);
|
| 442 |
|
|
#else
|
| 443 |
|
|
fprintf(stderr,
|
| 444 |
|
|
"Warning: boot image '%s' MBR is not a boot sector.\n",
|
| 445 |
|
|
de->whole_name);
|
| 446 |
|
|
#endif
|
| 447 |
|
|
}
|
| 448 |
|
|
/* find partition type */
|
| 449 |
|
|
boot_desc_entry->sys_type[0] = PARTITION_UNUSED;
|
| 450 |
|
|
for (i = 0; i < PARTITION_COUNT; ++i) {
|
| 451 |
|
|
int s_cyl_sec;
|
| 452 |
|
|
int e_cyl_sec;
|
| 453 |
|
|
|
| 454 |
|
|
s_cyl_sec =
|
| 455 |
|
|
la_to_u_2_byte(disk_mbr.partition[i].s_cyl_sec);
|
| 456 |
|
|
e_cyl_sec =
|
| 457 |
|
|
la_to_u_2_byte(disk_mbr.partition[i].e_cyl_sec);
|
| 458 |
|
|
|
| 459 |
|
|
if (disk_mbr.partition[i].type != PARTITION_UNUSED) {
|
| 460 |
|
|
if (boot_desc_entry->sys_type[0] !=
|
| 461 |
|
|
PARTITION_UNUSED) {
|
| 462 |
|
|
#ifdef USE_LIBSCHILY
|
| 463 |
|
|
comerrno(EX_BAD,
|
| 464 |
|
|
"Boot image '%s' has multiple partitions.\n",
|
| 465 |
|
|
de->whole_name);
|
| 466 |
|
|
#else
|
| 467 |
|
|
fprintf(stderr,
|
| 468 |
|
|
"Boot image '%s' has multiple partitions.\n",
|
| 469 |
|
|
de->whole_name);
|
| 470 |
|
|
exit(1);
|
| 471 |
|
|
#endif
|
| 472 |
|
|
}
|
| 473 |
|
|
boot_desc_entry->sys_type[0] =
|
| 474 |
|
|
disk_mbr.partition[i].type;
|
| 475 |
|
|
|
| 476 |
|
|
/* a few simple sanity warnings */
|
| 477 |
|
|
if (!boot_entry->not_bootable &&
|
| 478 |
|
|
disk_mbr.partition[i].status !=
|
| 479 |
|
|
PARTITION_ACTIVE) {
|
| 480 |
|
|
fprintf(stderr,
|
| 481 |
|
|
"Warning: partition not marked active.\n");
|
| 482 |
|
|
}
|
| 483 |
|
|
if (MBR_CYLINDER(s_cyl_sec) != 0 ||
|
| 484 |
|
|
disk_mbr.partition[i].s_head != 1 ||
|
| 485 |
|
|
MBR_SECTOR(s_cyl_sec != 1)) {
|
| 486 |
|
|
fprintf(stderr,
|
| 487 |
|
|
"Warning: partition does not start at 0/1/1.\n");
|
| 488 |
|
|
}
|
| 489 |
|
|
geosec = (MBR_CYLINDER(e_cyl_sec) + 1) *
|
| 490 |
|
|
(disk_mbr.partition[i].e_head + 1) *
|
| 491 |
|
|
MBR_SECTOR(e_cyl_sec);
|
| 492 |
|
|
if (geosec != nsectors) {
|
| 493 |
|
|
fprintf(stderr,
|
| 494 |
|
|
"Warning: image size does not match geometry (%d)\n",
|
| 495 |
|
|
geosec);
|
| 496 |
|
|
}
|
| 497 |
|
|
#ifdef DEBUG_TORITO
|
| 498 |
|
|
fprintf(stderr, "Partition start %u/%u/%u\n",
|
| 499 |
|
|
MBR_CYLINDER(s_cyl_sec),
|
| 500 |
|
|
disk_mbr.partition[i].s_head,
|
| 501 |
|
|
MBR_SECTOR(s_cyl_sec));
|
| 502 |
|
|
fprintf(stderr, "Partition end %u/%u/%u\n",
|
| 503 |
|
|
MBR_CYLINDER(e_cyl_sec),
|
| 504 |
|
|
disk_mbr.partition[i].e_head,
|
| 505 |
|
|
MBR_SECTOR(e_cyl_sec));
|
| 506 |
|
|
#endif
|
| 507 |
|
|
}
|
| 508 |
|
|
}
|
| 509 |
|
|
if (boot_desc_entry->sys_type[0] == PARTITION_UNUSED) {
|
| 510 |
|
|
#ifdef USE_LIBSCHILY
|
| 511 |
|
|
comerrno(EX_BAD,
|
| 512 |
|
|
"Boot image '%s' has no partitions.\n",
|
| 513 |
|
|
de->whole_name);
|
| 514 |
|
|
#else
|
| 515 |
|
|
fprintf(stderr,
|
| 516 |
|
|
"Boot image '%s' has no partitions.\n",
|
| 517 |
|
|
de->whole_name);
|
| 518 |
|
|
exit(1);
|
| 519 |
|
|
#endif
|
| 520 |
|
|
}
|
| 521 |
|
|
#ifdef DEBUG_TORITO
|
| 522 |
|
|
fprintf(stderr, "Partition type %u\n",
|
| 523 |
|
|
boot_desc_entry->sys_type[0]);
|
| 524 |
|
|
#endif
|
| 525 |
|
|
/* load single boot sector, in this case the MBR */
|
| 526 |
|
|
nsectors = 1;
|
| 527 |
|
|
|
| 528 |
|
|
} else if (boot_entry->no_emul_boot) {
|
| 529 |
|
|
/*
|
| 530 |
|
|
* no emulation is a simple image boot of all the sectors
|
| 531 |
|
|
* in the boot image
|
| 532 |
|
|
*/
|
| 533 |
|
|
boot_desc_entry->boot_media[0] = EL_TORITO_MEDIA_NOEMUL;
|
| 534 |
|
|
if (verbose > 0)
|
| 535 |
|
|
fprintf(stderr, "No emulation\n");
|
| 536 |
|
|
|
| 537 |
|
|
} else {
|
| 538 |
|
|
/* choose size of emulated floppy based on boot image size */
|
| 539 |
|
|
if (nsectors == 2880) {
|
| 540 |
|
|
boot_desc_entry->boot_media[0] = EL_TORITO_MEDIA_144FLOP;
|
| 541 |
|
|
if (verbose > 0)
|
| 542 |
|
|
fprintf(stderr, "Emulating a 1440 kB floppy\n");
|
| 543 |
|
|
|
| 544 |
|
|
} else if (nsectors == 5760) {
|
| 545 |
|
|
boot_desc_entry->boot_media[0] = EL_TORITO_MEDIA_288FLOP;
|
| 546 |
|
|
if (verbose > 0)
|
| 547 |
|
|
fprintf(stderr, "Emulating a 2880 kB floppy\n");
|
| 548 |
|
|
|
| 549 |
|
|
} else if (nsectors == 2400) {
|
| 550 |
|
|
boot_desc_entry->boot_media[0] = EL_TORITO_MEDIA_12FLOP;
|
| 551 |
|
|
if (verbose > 0)
|
| 552 |
|
|
fprintf(stderr, "Emulating a 1200 kB floppy\n");
|
| 553 |
|
|
|
| 554 |
|
|
} else {
|
| 555 |
|
|
#ifdef USE_LIBSCHILY
|
| 556 |
|
|
comerrno(EX_BAD,
|
| 557 |
|
|
"Error - boot image '%s' has not an allowable size.\n",
|
| 558 |
|
|
de->whole_name);
|
| 559 |
|
|
#else
|
| 560 |
|
|
fprintf(stderr,
|
| 561 |
|
|
"Error - boot image '%s' has not an allowable size.\n",
|
| 562 |
|
|
de->whole_name);
|
| 563 |
|
|
exit(1);
|
| 564 |
|
|
#endif
|
| 565 |
|
|
}
|
| 566 |
|
|
|
| 567 |
|
|
/* load single boot sector for floppies */
|
| 568 |
|
|
nsectors = 1;
|
| 569 |
|
|
}
|
| 570 |
|
|
|
| 571 |
|
|
/* fill in boot image details */
|
| 572 |
|
|
#ifdef DEBUG_TORITO
|
| 573 |
|
|
fprintf(stderr, "Boot %u sectors\n", nsectors);
|
| 574 |
|
|
fprintf(stderr, "Extent of boot images is %d\n",
|
| 575 |
|
|
get_733(de->isorec.extent));
|
| 576 |
|
|
#endif
|
| 577 |
|
|
set_721(boot_desc_entry->nsect, (unsigned int) nsectors);
|
| 578 |
|
|
set_731(boot_desc_entry->bootoff,
|
| 579 |
|
|
(unsigned int) get_733(de->isorec.extent));
|
| 580 |
|
|
|
| 581 |
|
|
|
| 582 |
|
|
/* If the user has asked for it, patch the boot image */
|
| 583 |
|
|
if (boot_entry->boot_info_table) {
|
| 584 |
|
|
int bootimage;
|
| 585 |
|
|
unsigned int bi_checksum;
|
| 586 |
|
|
unsigned int total_len;
|
| 587 |
|
|
static char csum_buffer[SECTOR_SIZE];
|
| 588 |
|
|
int len;
|
| 589 |
|
|
struct mkisofs_boot_info bi_table;
|
| 590 |
|
|
|
| 591 |
|
|
bootimage = open(de->whole_name, O_RDWR | O_BINARY);
|
| 592 |
|
|
if (bootimage == -1) {
|
| 593 |
|
|
#ifdef USE_LIBSCHILY
|
| 594 |
|
|
comerr(
|
| 595 |
|
|
"Error opening boot image file '%s' for update.\n",
|
| 596 |
|
|
de->whole_name);
|
| 597 |
|
|
#else
|
| 598 |
|
|
fprintf(stderr,
|
| 599 |
|
|
"Error opening boot image file '%s' for update.\n",
|
| 600 |
|
|
de->whole_name);
|
| 601 |
|
|
perror("");
|
| 602 |
|
|
exit(1);
|
| 603 |
|
|
#endif
|
| 604 |
|
|
}
|
| 605 |
|
|
/* Compute checksum of boot image, sans 64 bytes */
|
| 606 |
|
|
total_len = 0;
|
| 607 |
|
|
bi_checksum = 0;
|
| 608 |
|
|
while ((len = read(bootimage, csum_buffer, SECTOR_SIZE)) > 0) {
|
| 609 |
|
|
if (total_len & 3) {
|
| 610 |
|
|
#ifdef USE_LIBSCHILY
|
| 611 |
|
|
comerrno(EX_BAD,
|
| 612 |
|
|
"Odd alignment at non-end-of-file in boot image '%s'.\n",
|
| 613 |
|
|
de->whole_name);
|
| 614 |
|
|
#else
|
| 615 |
|
|
fprintf(stderr,
|
| 616 |
|
|
"Odd alignment at non-end-of-file in boot image '%s'.\n",
|
| 617 |
|
|
de->whole_name);
|
| 618 |
|
|
exit(1);
|
| 619 |
|
|
#endif
|
| 620 |
|
|
}
|
| 621 |
|
|
if (total_len < 64)
|
| 622 |
|
|
memset(csum_buffer, 0, 64 - total_len);
|
| 623 |
|
|
if (len < SECTOR_SIZE)
|
| 624 |
|
|
memset(csum_buffer + len, 0, SECTOR_SIZE-len);
|
| 625 |
|
|
for (i = 0; i < SECTOR_SIZE; i += 4)
|
| 626 |
|
|
bi_checksum += get_731(&csum_buffer[i]);
|
| 627 |
|
|
total_len += len;
|
| 628 |
|
|
}
|
| 629 |
|
|
|
| 630 |
|
|
if (total_len != de->size) {
|
| 631 |
|
|
#ifdef USE_LIBSCHILY
|
| 632 |
|
|
comerrno(EX_BAD,
|
| 633 |
|
|
"Boot image file '%s' changed underneath us!\n",
|
| 634 |
|
|
de->whole_name);
|
| 635 |
|
|
#else
|
| 636 |
|
|
fprintf(stderr,
|
| 637 |
|
|
"Boot image file '%s' changed underneath us!\n",
|
| 638 |
|
|
de->whole_name);
|
| 639 |
|
|
exit(1);
|
| 640 |
|
|
#endif
|
| 641 |
|
|
}
|
| 642 |
|
|
/* End of file, set position to byte 8 */
|
| 643 |
|
|
lseek(bootimage, (off_t)8, SEEK_SET);
|
| 644 |
|
|
memset(&bi_table, 0, sizeof (bi_table));
|
| 645 |
|
|
/* Is it always safe to assume PVD is at session_start+16? */
|
| 646 |
|
|
set_731(bi_table.bi_pvd, session_start + 16);
|
| 647 |
|
|
set_731(bi_table.bi_file, de->starting_block);
|
| 648 |
|
|
set_731(bi_table.bi_length, de->size);
|
| 649 |
|
|
set_731(bi_table.bi_csum, bi_checksum);
|
| 650 |
|
|
|
| 651 |
|
|
write(bootimage, &bi_table, sizeof (bi_table));
|
| 652 |
|
|
close(bootimage);
|
| 653 |
|
|
}
|
| 654 |
|
|
}/* fill_boot_desc(... */
|
| 655 |
|
|
|
| 656 |
|
|
void
|
| 657 |
|
|
get_boot_entry()
|
| 658 |
|
|
{
|
| 659 |
|
|
if (current_boot_entry)
|
| 660 |
|
|
return;
|
| 661 |
|
|
|
| 662 |
|
|
current_boot_entry = (struct eltorito_boot_entry_info *)
|
| 663 |
|
|
e_malloc(sizeof (struct eltorito_boot_entry_info));
|
| 664 |
|
|
memset(current_boot_entry, 0, sizeof (*current_boot_entry));
|
| 665 |
|
|
|
| 666 |
|
|
if (!first_boot_entry) {
|
| 667 |
|
|
first_boot_entry = current_boot_entry;
|
| 668 |
|
|
last_boot_entry = current_boot_entry;
|
| 669 |
|
|
} else {
|
| 670 |
|
|
last_boot_entry->next = current_boot_entry;
|
| 671 |
|
|
last_boot_entry = current_boot_entry;
|
| 672 |
|
|
}
|
| 673 |
|
|
}
|
| 674 |
|
|
|
| 675 |
|
|
void
|
| 676 |
|
|
new_boot_entry()
|
| 677 |
|
|
{
|
| 678 |
|
|
current_boot_entry = NULL;
|
| 679 |
|
|
}
|
| 680 |
|
|
|
| 681 |
|
|
/*
|
| 682 |
|
|
* Function to write the EVD for the disc.
|
| 683 |
|
|
*/
|
| 684 |
|
|
static int
|
| 685 |
|
|
tvd_write(outfile)
|
| 686 |
|
|
FILE *outfile;
|
| 687 |
|
|
{
|
| 688 |
|
|
/* check the boot image is not NULL */
|
| 689 |
|
|
if (!boot_image) {
|
| 690 |
|
|
#ifdef USE_LIBSCHILY
|
| 691 |
|
|
comerrno(EX_BAD, "No boot image specified.\n");
|
| 692 |
|
|
#else
|
| 693 |
|
|
fprintf(stderr, "No boot image specified.\n");
|
| 694 |
|
|
exit(1);
|
| 695 |
|
|
#endif
|
| 696 |
|
|
}
|
| 697 |
|
|
/* Next we write out the boot volume descriptor for the disc */
|
| 698 |
|
|
get_torito_desc(&gboot_desc);
|
| 699 |
|
|
xfwrite(&gboot_desc, SECTOR_SIZE, 1, outfile, 0, FALSE);
|
| 700 |
|
|
last_extent_written++;
|
| 701 |
|
|
return (0);
|
| 702 |
|
|
}
|
| 703 |
|
|
|
| 704 |
|
|
struct output_fragment torito_desc = {NULL, oneblock_size, NULL, tvd_write, "Eltorito Volume Descriptor"};
|