pci: don't do sanity check for missing pci bus, the check can misfire.
[minix.git] / commands / simple / writeisofs.c
blob0a66d9d958e54e31a8a21edeaed9dd0643d33812
2 /* writeisofs - simple ISO9660-format-image writing utility */
4 #include <errno.h>
5 #include <stdio.h>
6 #include <time.h>
7 #include <stdlib.h>
8 #include <fcntl.h>
9 #include <string.h>
10 #include <unistd.h>
11 #include <dirent.h>
12 #include <assert.h>
13 #include <ctype.h>
14 #include <a.out.h>
15 #include <machine/partition.h>
17 #include <sys/stat.h>
19 #define Writefield(fd, f) Write(fd, &(f), sizeof(f))
21 extern char *optarg;
22 extern int optind;
24 typedef unsigned char u_int8_t;
25 typedef unsigned short int u_int16_t;
26 typedef unsigned long int u_int32_t;
28 #ifndef min
29 #define min(a,b) ((a) < (b) ? (a) : (b))
30 #endif
32 #define FLAG_DIR 2
34 #include <sys/types.h>
35 #include <sys/stat.h>
37 #define NAMELEN (DIRSIZ+5)
38 #define ISONAMELEN 12
39 #define PLATFORM_80X86 0
41 #define ISO_SECTOR 2048
42 #define VIRTUAL_SECTOR 512
43 #define ROUNDUP(v, n) (((v)+(n)-1)/(n))
45 #define CURRENTDIR "."
46 #define PARENTDIR ".."
48 /* *** CD (disk) data structures ********************* */
50 /* primary volume descriptor */
52 struct pvd {
53 u_int8_t one;
54 char set[6];
55 u_int8_t zero;
56 char system[32];
57 char volume[32];
58 u_int8_t zeroes1[8];
59 u_int32_t sectors[2];
60 u_int8_t zeroes2[32];
61 u_int16_t setsize[2];
62 u_int16_t seq[2];
63 u_int16_t sectorsize[2];
64 u_int32_t pathtable[2];
65 u_int32_t first_little_pathtable_start;
66 u_int32_t second_little_pathtable_start;
67 u_int32_t first_big_pathtable_start;
68 u_int32_t second_big_pathtable_start;
69 u_int8_t rootrecord[34];
70 u_int8_t volumeset[128];
71 u_int8_t publisher[128];
72 u_int8_t preparer[128];
73 u_int8_t application[128];
74 u_int8_t copyrightfile[37];
75 u_int8_t abstractfile[37];
76 u_int8_t bibliofile[37];
77 u_int8_t create[17];
78 u_int8_t modified[17];
79 char expiry[17];
80 u_int8_t effective[17];
81 u_int8_t one2;
82 u_int8_t zero2;
83 u_int8_t zeroes3[512];
84 u_int8_t zeroes4[653];
87 /* boot record volume descriptor */
89 struct bootrecord {
90 u_int8_t indicator; /* 0 */
91 char set[5]; /* "CD001" */
92 u_int8_t version; /* 1 */
93 char ident[32]; /* "EL TORITO SPECIFICATION" */
94 u_int8_t zero[32]; /* unused, must be 0 */
95 u_int32_t bootcatalog; /* starting sector of boot catalog */
96 u_int8_t zero2[1973]; /* unused, must be 0 */
99 /* boot catalog validation entry */
101 struct bc_validation {
102 u_int8_t headerid; /* 1 */
103 u_int8_t platform; /* 0: 80x86; 1: powerpc; 2: mac */
104 u_int8_t zero[2]; /* unused, must be 0 */
105 char idstring[24]; /* id string */
106 u_int16_t checksum;
107 u_int8_t keys[2]; /* 0x55AA */
110 /* boot catalog initial/default entry */
112 #define INDICATE_BOOTABLE 0x88
114 #define BOOTMEDIA_UNSPECIFIED -1
115 #define BOOTMEDIA_NONE 0
116 #define BOOTMEDIA_120M 1
117 #define BOOTMEDIA_144M 2
118 #define BOOTMEDIA_288M 3
119 #define BOOTMEDIA_HARDDISK 4
121 struct bc_initial {
122 u_int8_t indicator; /* INDICATE_BOOTABLE */
123 u_int8_t media; /* BOOTMEDIA_* */
124 u_int16_t seg; /* load segment or 0 for default */
125 u_int8_t type; /* system type (from part. table) */
126 u_int8_t zero;
127 u_int16_t sectors;
128 u_int32_t startsector;
129 u_int8_t zero2[20];
132 /* directory entry */
134 struct dir {
135 u_int8_t recordsize;
136 u_int8_t extended;
137 u_int32_t datasector[2];
138 u_int32_t filesize[2];
139 u_int8_t year;
140 u_int8_t month;
141 u_int8_t day;
142 u_int8_t hour;
143 u_int8_t minute;
144 u_int8_t second;
145 u_int8_t offset;
146 u_int8_t flags;
147 u_int8_t interleaved;
148 u_int8_t interleavegap;
149 u_int16_t sequence[2];
150 u_int8_t namelen;
151 char name[NAMELEN];
154 /* *** program (memory) data structures ********************* */
156 struct node {
157 char name[NAMELEN];
158 int isdir;
159 int pathtablerecord;
160 struct node *firstchild, *nextchild;
162 /* filled out at i/o time */
163 u_int32_t startsector, bytesize;
166 int n_reserved_pathtableentries = 0, n_used_pathtableentries = 0;
167 int bootmedia = BOOTMEDIA_UNSPECIFIED;
168 unsigned long bootseg = 0;
169 int system_type = 0;
171 int get_system_type(int fd);
173 ssize_t
174 Write(int fd, void *buf, ssize_t len)
176 ssize_t r;
177 if((r=write(fd, buf, len)) != len) {
178 if(r < 0) { perror("write"); }
179 fprintf(stderr, "failed or short write - aborting.\n");
180 exit(1);
182 return len;
185 off_t
186 Lseek(int fd, off_t pos, int rel)
188 off_t r;
190 if((r=lseek(fd, pos, rel)) < 0) {
191 perror("lseek");
192 fprintf(stderr, "lseek failed - aborting.\n");
193 exit(1);
196 return r;
199 void
200 writesector(int fd, char *block, int *currentsector)
202 Write(fd, block, ISO_SECTOR);
203 (*currentsector)++;
204 return;
207 void
208 seeksector(int fd, int sector, int *currentsector)
210 Lseek(fd, sector*ISO_SECTOR, SEEK_SET);
211 *currentsector = sector;
214 void
215 seekwritesector(int fd, int sector, char *block, int *currentsector)
217 seeksector(fd, sector, currentsector);
218 writesector(fd, block, currentsector);
221 ssize_t
222 Read(int fd, void *buf, ssize_t len)
224 ssize_t r;
225 if((r=read(fd, buf, len)) != len) {
226 if(r < 0) { perror("read"); }
227 fprintf(stderr, "failed or short read.\n");
228 exit(1);
231 return len;
234 void both16(unsigned char *both, unsigned short i16)
236 unsigned char *little, *big;
238 little = both;
239 big = both + 2;
241 little[0] = big[1] = i16 & 0xFF;
242 little[1] = big[0] = (i16 >> 8) & 0xFF;
245 void both32(unsigned char *both, unsigned long i32)
247 unsigned char *little, *big;
249 little = both;
250 big = both + 4;
252 little[0] = big[3] = i32 & 0xFF;
253 little[1] = big[2] = (i32 >> 8) & 0xFF;
254 little[2] = big[1] = (i32 >> 16) & 0xFF;
255 little[3] = big[0] = (i32 >> 24) & 0xFF;
258 #define MINDIRLEN 1
259 #define MAXDIRLEN 31
261 #define MAXLEVEL 8
263 static int cmpf(const void *v1, const void *v2)
265 struct node *n1, *n2;
266 int i;
267 char f1[NAMELEN], f2[NAMELEN];
269 n1 = (struct node *) v1;
270 n2 = (struct node *) v2;
271 strcpy(f1, n1->name);
272 strcpy(f2, n2->name);
273 for(i = 0; i < strlen(f1); i++) f1[i] = toupper(f1[i]);
274 for(i = 0; i < strlen(f2); i++) f2[i] = toupper(f2[i]);
277 return -strcmp(f1, f2);
280 void
281 maketree(struct node *thisdir, char *name, int level)
283 DIR *dir;
284 struct dirent *e;
285 struct node *dirnodes = NULL;
286 int reserved_dirnodes = 0, used_dirnodes = 0;
287 struct node *child;
289 thisdir->firstchild = NULL;
290 thisdir->isdir = 1;
291 thisdir->startsector = 0xdeadbeef;
293 if(level >= MAXLEVEL) {
294 fprintf(stderr, "ignoring entries in %s (too deep for iso9660)\n",
295 name);
296 return;
299 if(!(dir = opendir(CURRENTDIR))) {
300 perror("opendir");
301 return;
304 /* how many entries do we need to allocate? */
305 while(readdir(dir)) reserved_dirnodes++;
306 if(!reserved_dirnodes) {
307 closedir(dir);
308 return;
311 if(!(dirnodes = malloc(sizeof(*dirnodes)*reserved_dirnodes))) {
312 fprintf(stderr, "couldn't allocate dirnodes (%d bytes)\n",
313 sizeof(*dirnodes)*reserved_dirnodes);
314 exit(1);
318 /* remember all entries in this dir */
319 rewinddir(dir);
321 child = dirnodes;
322 while((e=readdir(dir))) {
323 struct stat st;
324 mode_t type;
325 if(!strcmp(e->d_name, CURRENTDIR) || !strcmp(e->d_name, PARENTDIR))
326 continue;
327 if(stat(e->d_name, &st) < 0) {
328 perror(e->d_name);
329 fprintf(stderr, "failed to stat file/dir\n");
330 exit(1);
333 type = st.st_mode & S_IFMT;
336 printf("%s type: %x dir: %x file: %x\n",
337 e->d_name, type, S_IFDIR, S_IFREG);
339 if(type != S_IFDIR && type != S_IFREG)
340 continue;
342 used_dirnodes++;
343 if(used_dirnodes > reserved_dirnodes) {
344 fprintf(stderr, "huh, directory entries appeared "
345 "(not enough pre-allocated nodes; this can't happen) ?\n");
346 exit(1);
349 if(type == S_IFDIR) {
350 child->isdir = 1;
351 } else {
352 child->isdir = 0;
353 child->firstchild = NULL;
355 strncpy(child->name, e->d_name, sizeof(child->name));
357 child++;
360 closedir(dir);
362 if(!used_dirnodes)
363 return;
365 if(!(dirnodes=realloc(dirnodes, used_dirnodes*sizeof(*dirnodes)))) {
366 fprintf(stderr, "realloc() of dirnodes failed - aborting\n");
367 exit(1);
370 qsort(dirnodes, used_dirnodes, sizeof(*dirnodes), cmpf);
372 child = dirnodes;
374 while(used_dirnodes--) {
375 child->nextchild = thisdir->firstchild;
376 thisdir->firstchild = child;
377 if(child->isdir) {
378 if(chdir(child->name) < 0) {
379 perror(child->name);
380 } else {
381 maketree(child, child->name, level+1);
382 if(chdir(PARENTDIR) < 0) {
383 perror("chdir() failed");
384 fprintf(stderr, "couldn't chdir() to parent, aborting\n");
385 exit(1);
390 child++;
395 void
396 little32(unsigned char *dest, u_int32_t src)
398 dest[0] = ((src >> 0) & 0xFF);
399 dest[1] = ((src >> 8) & 0xFF);
400 dest[2] = ((src >> 16) & 0xFF);
401 dest[3] = ((src >> 24) & 0xFF);
403 return;
406 void
407 little16(unsigned char *dest, u_int16_t src)
409 dest[0] = ((src >> 0) & 0xFF);
410 dest[1] = ((src >> 8) & 0xFF);
412 return;
415 void
416 big32(unsigned char *dest, u_int32_t src)
418 dest[3] = ((src >> 0) & 0xFF);
419 dest[2] = ((src >> 8) & 0xFF);
420 dest[1] = ((src >> 16) & 0xFF);
421 dest[0] = ((src >> 24) & 0xFF);
422 return;
425 void
426 big16(unsigned char *dest, u_int16_t src)
428 dest[1] = ((src >> 0) & 0xFF);
429 dest[0] = ((src >> 8) & 0xFF);
430 return;
434 void
435 traversetree(struct node *root, int level, int littleendian,
436 int maxlevel, int *bytes, int fd, int parentrecord, int *recordno)
438 struct node *child;
439 struct pte {
440 u_int8_t len;
441 u_int8_t zero;
442 u_int32_t startsector;
443 u_int16_t parent;
444 } pte;
446 if(level == maxlevel) {
447 int i;
448 char newname[NAMELEN];
449 if(!root->isdir)
450 return;
451 pte.zero = 0;
452 if(level == 1) {
453 /* root */
454 pte.len = 1;
455 pte.parent = 1;
456 root->name[0] = root->name[1] = '\0';
457 } else {
458 pte.len = strlen(root->name);
459 pte.parent = parentrecord;
461 pte.startsector = root->startsector;
462 root->pathtablerecord = (*recordno)++;
464 if(littleendian) {
465 little32((unsigned char *) &pte.startsector, pte.startsector);
466 little16((unsigned char *) &pte.parent, pte.parent);
467 } else {
468 big32((unsigned char *) &pte.startsector, pte.startsector);
469 big16((unsigned char *) &pte.parent, pte.parent);
472 *bytes += Write(fd, &pte.len, sizeof(pte.len));
473 *bytes += Write(fd, &pte.zero, sizeof(pte.zero));
474 *bytes += Write(fd, &pte.startsector, sizeof(pte.startsector));
475 *bytes += Write(fd, &pte.parent, sizeof(pte.parent));
476 if(!(pte.len%2))
477 root->name[pte.len++] = '\0';
478 for(i = 0; i < pte.len; i++)
479 newname[i] = toupper(root->name[i]);
480 *bytes += Write(fd, newname, pte.len);
481 return;
484 for(child = root->firstchild; child; child = child->nextchild)
485 if(child->isdir)
486 traversetree(child, level+1, littleendian,
487 maxlevel, bytes, fd, root->pathtablerecord,
488 recordno);
490 return;
494 makepathtables(struct node *root, int littleendian, int *bytes, int fd)
496 int level;
497 static char block[ISO_SECTOR];
498 int recordno;
500 recordno = 1;
502 *bytes = 0;
504 for(level = 1; level <= MAXLEVEL; level++)
505 traversetree(root, 1, littleendian, level, bytes, fd, 1, &recordno);
507 if(*bytes % ISO_SECTOR) {
508 ssize_t x;
509 x = ISO_SECTOR-(*bytes % ISO_SECTOR);
510 write(fd, block, x);
511 *bytes += x;
514 return *bytes/ISO_SECTOR;
517 ssize_t
518 write_direntry(char *origname, u_int32_t sector, u_int32_t size, int isdir,
519 int fd)
521 int namelen, total = 0;
522 struct dir entry;
523 char copyname[NAMELEN];
525 memset(&entry, 0, sizeof(entry));
527 if(!strcmp(origname, CURRENTDIR)) {
528 namelen = 1;
529 } else if(!strcmp(origname, PARENTDIR)) {
530 entry.name[0] = '\001';
531 namelen = 1;
532 } else {
533 int i;
534 strcpy(copyname, origname);
535 namelen = strlen(copyname);
537 if(namelen > ISONAMELEN) {
538 fprintf(stderr, "%s: truncated, too long for iso9660\n", copyname);
539 namelen = ISONAMELEN;
540 copyname[namelen] = '\0';
543 strcpy(entry.name, copyname);
544 for(i = 0; i < namelen; i++)
545 entry.name[i] = toupper(entry.name[i]);
547 /* padding byte + system field */
548 entry.name[namelen] = '\0';
549 entry.name[namelen+1] = '\0';
550 entry.name[namelen+2] = '\0';
552 entry.namelen = namelen; /* original length */
553 if(!(namelen%2)) namelen++; /* length with padding byte */
556 /* XXX 2 extra bytes for 'system use'.. */
557 entry.recordsize = 33 + namelen;
558 both32((unsigned char *) entry.datasector, sector);
559 both32((unsigned char *) entry.filesize, size);
561 if(isdir) entry.flags = FLAG_DIR;
563 /* XXX node date */
565 both16((unsigned char *) entry.sequence, 1);
567 total = Write(fd, &entry.recordsize, sizeof(entry.recordsize));
568 total += Write(fd, &entry.extended, sizeof(entry.extended));
569 total += Write(fd, entry.datasector, sizeof(entry.datasector));
570 total += Write(fd, entry.filesize, sizeof(entry.filesize));
571 total += Write(fd, &entry.year, sizeof(entry.year));
572 total += Write(fd, &entry.month, sizeof(entry.month));
573 total += Write(fd, &entry.day, sizeof(entry.day));
574 total += Write(fd, &entry.hour, sizeof(entry.hour));
575 total += Write(fd, &entry.minute, sizeof(entry.minute));
576 total += Write(fd, &entry.second, sizeof(entry.second));
577 total += Write(fd, &entry.offset, sizeof(entry.offset));
578 total += Write(fd, &entry.flags, sizeof(entry.flags));
579 total += Write(fd, &entry.interleaved, sizeof(entry.interleaved));
580 total += Write(fd, &entry.interleavegap, sizeof(entry.interleavegap));
581 total += Write(fd, entry.sequence, sizeof(entry.sequence));
582 total += Write(fd, &entry.namelen, sizeof(entry.namelen));
583 total += Write(fd, entry.name, namelen);
585 if(total != entry.recordsize || (total % 2) != 0) {
586 printf("%2d, %2d! ", total, entry.recordsize);
587 printf("%3d = %3d - %2d + %2d\n",
588 entry.recordsize, sizeof(entry), sizeof(entry.name), namelen);
591 return entry.recordsize;
594 void
595 writedata(struct node *parent, struct node *root,
596 int fd, int *currentsector, int dirs, struct dir *rootentry,
597 int rootsize, int remove_after)
599 static char buf[1024*1024];
600 struct node *c;
601 ssize_t written = 0, rest;
603 for(c = root->firstchild; c; c = c->nextchild) {
604 if(c->isdir && chdir(c->name) < 0) {
605 perror(c->name);
606 fprintf(stderr, "couldn't chdir to %s - aborting\n",
607 c->name);
608 exit(1);
610 writedata(root, c, fd, currentsector, dirs, rootentry, rootsize, remove_after);
611 if(c->isdir && chdir(PARENTDIR) < 0) {
612 perror("chdir to ..");
613 fprintf(stderr, "couldn't chdir to parent - "
614 "aborting\n");
615 exit(1);
619 /* write nodes depth-first, down-top */
621 if(root->isdir && dirs) {
622 /* dir */
623 written = 0;
624 root->startsector = *currentsector;
625 written += write_direntry(CURRENTDIR, root->startsector,
626 root->bytesize, root->isdir, fd);
627 if(parent) {
628 written += write_direntry(PARENTDIR, parent->startsector,
629 root->bytesize, root->isdir, fd);
630 } else {
631 written += write_direntry(PARENTDIR, root->startsector,
632 root->bytesize, root->isdir, fd);
634 for(c = root->firstchild; c; c = c->nextchild) {
635 off_t cur1, cur2;
636 ssize_t written_before;
637 cur1 = Lseek(fd, 0, SEEK_CUR);
638 written_before = written;
639 written += write_direntry(c->name,
640 c->startsector, c->bytesize, c->isdir, fd);
641 cur2 = Lseek(fd, 0, SEEK_CUR);
642 if(cur1/ISO_SECTOR != (cur2-1)/ISO_SECTOR) {
643 /* passed a sector boundary, argh! */
644 Lseek(fd, cur1, SEEK_SET);
645 written = written_before;
646 rest=(ISO_SECTOR-(written % ISO_SECTOR));
647 memset(buf, 0, rest);
648 Write(fd, buf, rest);
649 written += rest;
650 written += write_direntry(c->name,
651 c->startsector, c->bytesize, c->isdir, fd);
654 root->bytesize = written;
655 } else if(!root->isdir && !dirs) {
656 /* file */
657 struct stat st;
658 ssize_t rem;
659 int filefd;
661 if(stat(root->name, &st) < 0) {
662 perror(root->name);
663 fprintf(stderr, "couldn't stat %s - aborting\n", root->name);
664 exit(1);
667 if((filefd = open(root->name, O_RDONLY)) < 0) {
668 perror(root->name);
669 fprintf(stderr, "couldn't open %s - aborting\n", root->name);
670 exit(1);
673 rem = st.st_size;
675 root->startsector = *currentsector;
677 while(rem > 0) {
678 ssize_t chunk;
679 chunk = min(sizeof(buf), rem);
680 Read(filefd, buf, chunk);
681 Write(fd, buf, chunk);
682 rem -= chunk;
685 close(filefd);
687 root->bytesize = written = st.st_size;
688 if(remove_after && unlink(root->name) < 0) {
689 perror("unlink");
690 fprintf(stderr, "couldn't remove %s\n", root->name);
692 } else {
693 /* nothing to be done */
694 return;
697 /* fill out sector with zero bytes */
699 if((rest=(ISO_SECTOR-(written % ISO_SECTOR)))) {
700 memset(buf, 0, rest);
701 Write(fd, buf, rest);
702 written += rest;
705 /* update dir size with padded size */
707 if(root->isdir) { root->bytesize = written; }
709 *currentsector += written/ISO_SECTOR;
712 void
713 writebootcatalog(int fd, int *currentsector, int imagesector, int imagesectors)
715 static char buf[ISO_SECTOR];
716 struct bc_validation validate;
717 struct bc_initial initial;
719 ssize_t written, rest;
720 u_int16_t *v, sum = 0;
721 int i;
723 /* write validation entry */
725 memset(&validate, 0, sizeof(validate));
726 validate.headerid = 1;
727 validate.platform = PLATFORM_80X86;
728 strcpy(validate.idstring, "");
729 validate.keys[0] = 0x55;
730 validate.keys[1] = 0xaa;
732 v = (u_int16_t *) &validate;
733 for(i = 0; i < sizeof(validate)/2; i++)
734 sum += v[i];
735 validate.checksum = 65535 - sum + 1; /* sum must be 0 */
737 written = Write(fd, &validate, sizeof(validate));
739 /* write initial/default entry */
741 memset(&initial, 0, sizeof(initial));
743 initial.indicator = INDICATE_BOOTABLE;
744 initial.media = bootmedia;
745 initial.seg = (u_int16_t) (bootseg & 0xFFFF);
746 initial.sectors = 1;
747 if (bootmedia == BOOTMEDIA_HARDDISK)
749 initial.type = system_type;
751 if (bootmedia == BOOTMEDIA_NONE)
753 initial.sectors = imagesectors;
755 initial.startsector = imagesector;
757 written += Write(fd, &initial, sizeof(initial));
759 /* fill out the rest of the sector with 0's */
761 if((rest = ISO_SECTOR - (written % ISO_SECTOR))) {
762 memset(buf, 0, sizeof(buf));
763 written += Write(fd, buf, rest);
766 (*currentsector) += written / ISO_SECTOR;
768 return;
772 writebootimage(char *bootimage, int bootfd, int fd, int *currentsector,
773 char *appendsectorinfo, struct node *root)
775 static unsigned char buf[1024*64], *addr;
776 ssize_t written = 0, rest;
777 int virtuals, rem;
778 struct exec hdr;
779 struct bap {
780 off_t sector;
781 int length;
782 } bap[2];
784 bap[1].length = bap[1].sector = 0;
786 Read(bootfd, &hdr, A_MINHDR);
788 if(hdr.a_magic[0] != A_MAGIC0) {
789 fprintf(stderr, "bad magic in a.out of boot image.\n");
790 exit(1);
793 if(hdr.a_hdrlen > sizeof(hdr)) {
794 fprintf(stderr, "surprisingly large header in boot image.\n");
795 exit(1);
798 /* read rest of a.out header. */
799 Read(bootfd, (char *) &hdr + A_MINHDR, hdr.a_hdrlen - A_MINHDR);
801 /* copy text+data */
802 rem = hdr.a_text + hdr.a_data;
804 while(rem > 0) {
805 int want;
806 want = rem < sizeof(buf) ? rem : sizeof(buf);
807 Read(bootfd, buf, want);
808 written += Write(fd, buf, want);
809 rem -= want;
812 if(appendsectorinfo) {
813 struct node *n;
814 for(n = root->firstchild; n; n = n ->nextchild) {
815 if(!strcasecmp(appendsectorinfo, n->name)) {
816 bap[0].sector = n->startsector;
817 bap[0].length = ROUNDUP(n->bytesize, ISO_SECTOR);
818 break;
821 if(!n) {
822 fprintf(stderr, "%s not found in root.\n",
823 appendsectorinfo);
824 exit(1);
827 fprintf(stderr, " * appended sector info: 0x%lx len 0x%lx\n",
828 bap[0].sector, bap[0].length);
831 addr = buf;
832 addr[0] = bap[0].length; assert(addr[0] > 0);
833 addr[1] = (bap[0].sector >> 0) & 0xFF;
834 addr[2] = (bap[0].sector >> 8) & 0xFF;
835 addr[3] = (bap[0].sector >> 16) & 0xFF;
836 addr[4] = 0;
837 addr[5] = 0;
839 /* Always save space for sector info after the boot image. */
840 written += Write(fd, addr, 6);
842 virtuals = ROUNDUP(written, VIRTUAL_SECTOR);
843 assert(virtuals * VIRTUAL_SECTOR >= written);
845 if((rest = ISO_SECTOR - (written % ISO_SECTOR))) {
846 memset(buf, 0, sizeof(buf));
847 written += Write(fd, buf, rest);
850 (*currentsector) += written/ISO_SECTOR;
852 return virtuals;
855 void
856 writebootrecord(int fd, int *currentsector, int bootcatalogsector)
858 int i;
859 static struct bootrecord bootrecord;
860 ssize_t w = 0;
861 /* boot record volume descriptor */
863 memset(&bootrecord, 0, sizeof(bootrecord));
864 bootrecord.set[0] = 'C';
865 bootrecord.set[1] = 'D';
866 bootrecord.set[2] = '0';
867 bootrecord.set[3] = '0';
868 bootrecord.set[4] = '1';
869 bootrecord.version = 1;
870 bootrecord.bootcatalog = bootcatalogsector;
871 strcpy(bootrecord.ident, "EL TORITO SPECIFICATION");
872 for(i = strlen(bootrecord.ident);
873 i < sizeof(bootrecord.ident); i++)
874 bootrecord.ident[i] = '\0';
876 w = Writefield(fd, bootrecord.indicator);
877 w += Writefield(fd, bootrecord.set);
878 w += Writefield(fd, bootrecord.version);
879 w += Writefield(fd, bootrecord.ident);
880 w += Writefield(fd, bootrecord.zero);
881 w += Writefield(fd, bootrecord.bootcatalog);
882 w += Writefield(fd, bootrecord.zero2);
884 if(w != ISO_SECTOR) {
885 fprintf(stderr, "WARNING: something went wrong - boot record (%d) isn't a sector size (%d)\n",
886 w, ISO_SECTOR);
889 (*currentsector)++;
893 main(int argc, char *argv[])
895 int currentsector = 0;
896 int imagesector, imagesectors;
897 int bootfd, fd, i, ch, nsectors;
898 int remove_after = 0;
899 static char block[ISO_SECTOR];
900 static struct pvd pvd;
901 char *label = "ISO9660";
902 struct tm *now;
903 time_t nowtime;
904 char timestr[20], *prog;
905 char *bootimage = NULL;
906 struct node root;
907 int pvdsector;
908 int bigpath, littlepath, pathbytes = 0, dirsector, filesector, enddir;
909 int bootvolumesector, bootcatalogsector;
910 char *appendsectorinfo = NULL;
912 prog = argv[0];
914 /* This check is to prevent compiler padding screwing up
915 * our format.
918 if(sizeof(struct pvd) != ISO_SECTOR) {
919 fprintf(stderr, "Something confusing happened at\n"
920 "compile-time; pvd should be a sector size. %d != %d\n",
921 sizeof(struct pvd), ISO_SECTOR);
922 return 1;
925 while ((ch = getopt(argc, argv, "a:b:s:Rb:hl:nf")) != -1) {
926 switch(ch) {
927 case 's':
928 if(optarg[0] != '0' || optarg[1] != 'x') {
929 fprintf(stderr, "%s: -s<hex>\n",
930 argv[0]);
931 return 1;
933 bootseg = strtoul(optarg+2, NULL, 16);
934 break;
935 case 'h':
936 bootmedia= BOOTMEDIA_HARDDISK;
937 break;
938 case 'n':
939 bootmedia= BOOTMEDIA_NONE;
940 break;
941 case 'f':
942 bootmedia= BOOTMEDIA_144M;
943 break;
944 case 'a':
945 if(!(appendsectorinfo = strdup(optarg)))
946 exit(1);
947 break;
948 case 'l':
949 label = optarg;
950 break;
951 case 'r':
952 remove_after = 1;
953 break;
954 case 'b':
955 bootimage = optarg;
956 if((bootfd = open(bootimage, O_RDONLY)) < 0) {
957 perror(bootimage);
958 return 1;
960 break;
964 argc -= optind;
965 argv += optind;
967 /* Args check */
969 if(argc != 2) {
970 fprintf(stderr, "usage: %s [-l <label>] [-b <bootimage> [-n] [-f] [-h] [-s <bootsegment>] [ -a <appendfile> ] <dir> <isofile>\n",
971 prog);
972 return 1;
975 if((bootimage && bootmedia == BOOTMEDIA_UNSPECIFIED) ||
976 (!bootimage && bootmedia != BOOTMEDIA_UNSPECIFIED)) {
977 fprintf(stderr, "%s: provide both boot image and boot type or neither.\n",
978 prog);
979 return 1;
982 if(!bootimage && bootseg) {
983 fprintf(stderr, "%s: boot seg provided but no boot image\n",
984 prog);
985 return 1;
988 if(appendsectorinfo) {
989 if(!bootimage || bootmedia != BOOTMEDIA_NONE) {
990 fprintf(stderr, "%s: append sector info where?\n",
991 prog);
992 return 1;
996 /* create .iso file */
998 if((fd=open(argv[1], O_WRONLY | O_TRUNC | O_CREAT, 0600)) < 0) {
999 perror(argv[1]);
1000 return 1;
1003 /* go to where the iso has to be made from */
1005 if(chdir(argv[0]) < 0) {
1006 perror(argv[0]);
1007 return 1;
1010 /* collect dirs and files */
1012 fprintf(stderr, " * traversing input tree\n");
1014 maketree(&root, "", 1);
1016 fprintf(stderr, " * writing initial zeroes and pvd\n");
1018 /* first sixteen sectors are zero */
1020 memset(block, 0, sizeof(block));
1022 for(i = 0; i < 16; i++)
1023 writesector(fd, block, &currentsector);
1025 /* Primary Volume Descriptor */
1026 memset(&pvd, 0, sizeof(pvd));
1027 pvd.one = 1;
1028 pvd.set[0] = 67;
1029 pvd.set[1] = 68;
1030 pvd.set[2] = 48;
1031 pvd.set[3] = 48;
1032 pvd.set[4] = 49;
1033 pvd.set[5] = 1;
1034 pvd.set[5] = 1;
1036 strncpy(pvd.volume, label, sizeof(pvd.volume)-1);
1037 for(i = strlen(pvd.volume); i < sizeof(pvd.volume); i++)
1038 pvd.volume[i] = ' ';
1039 for(i = 0; i < sizeof(pvd.system); i++)
1040 pvd.system[i] = ' ';
1042 both16((unsigned char *) pvd.setsize, 1);
1043 both16((unsigned char *) pvd.seq, 1);
1044 both16((unsigned char *) pvd.sectorsize, ISO_SECTOR);
1046 /* fill time fields */
1047 time(&nowtime);
1048 now = gmtime(&nowtime);
1049 strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S000", now);
1050 memcpy(pvd.create, timestr, strlen(timestr));
1051 memcpy(pvd.modified, timestr, strlen(timestr));
1052 memcpy(pvd.effective, timestr, strlen(timestr));
1053 strcpy(pvd.expiry, "0000000000000000"); /* not specified */
1054 pvdsector = currentsector;
1056 writesector(fd, (char *) &pvd, &currentsector);
1058 if(bootimage) {
1059 fprintf(stderr, " * writing boot record volume descriptor\n");
1060 bootvolumesector = currentsector;
1061 writebootrecord(fd, &currentsector, 0);
1064 /* volume descriptor set terminator */
1065 memset(block, 0, sizeof(block));
1066 block[0] = 255;
1067 block[1] = 67;
1068 block[2] = 68;
1069 block[3] = 48;
1070 block[4] = 48;
1071 block[5] = 49;
1072 block[6] = 1;
1074 writesector(fd, block, &currentsector);
1076 if(bootimage) {
1077 /* write the boot catalog */
1078 fprintf(stderr, " * writing the boot catalog\n");
1079 bootcatalogsector = currentsector;
1080 if (bootmedia == BOOTMEDIA_HARDDISK)
1081 system_type = get_system_type(bootfd);
1082 writebootcatalog(fd, &currentsector, 0, 0);
1084 /* write boot image */
1085 fprintf(stderr, " * writing the boot image\n");
1086 imagesector = currentsector;
1087 imagesectors = writebootimage(bootimage, bootfd,
1088 fd, &currentsector, NULL, &root);
1089 fprintf(stderr, " * image: %d %d-byte sectors @ cd sector 0x%x\n",
1090 imagesectors, VIRTUAL_SECTOR, imagesector);
1093 /* write out all the file data */
1095 filesector = currentsector;
1096 fprintf(stderr, " * writing file data\n");
1097 writedata(NULL, &root, fd, &currentsector, 0,
1098 (struct dir *) &pvd.rootrecord, sizeof(pvd.rootrecord),
1099 remove_after);
1101 /* write out all the dir data */
1103 dirsector = currentsector;
1104 fprintf(stderr, " * writing dir data\n");
1105 writedata(NULL, &root, fd, &currentsector, 1,
1106 (struct dir *) &pvd.rootrecord, sizeof(pvd.rootrecord),
1107 remove_after);
1108 enddir = currentsector;
1109 seeksector(fd, dirsector, &currentsector);
1110 fprintf(stderr, " * rewriting dir data\n");
1111 fflush(NULL);
1112 writedata(NULL, &root, fd, &currentsector, 1,
1113 (struct dir *) &pvd.rootrecord, sizeof(pvd.rootrecord),
1114 remove_after);
1115 if(currentsector != enddir) {
1116 fprintf(stderr, "warning: inconsistent directories - "
1117 "I have a bug! iso may be broken.\n");
1120 /* now write the path table in both formats */
1122 fprintf(stderr, " * writing big-endian path table\n");
1123 bigpath = currentsector;
1124 currentsector += makepathtables(&root, 0, &pathbytes, fd);
1126 fprintf(stderr, " * writing little-endian path table\n");
1127 littlepath = currentsector;
1128 currentsector += makepathtables(&root, 1, &pathbytes, fd);
1130 /* this is the size of the iso filesystem for use in the pvd later */
1132 nsectors = currentsector;
1133 both32((unsigned char *) pvd.sectors, nsectors);
1135 /* *********** Filesystem writing done ************************* */
1137 /* finish and rewrite the pvd. */
1138 fprintf(stderr, " * rewriting pvd\n");
1139 seekwritesector(fd, pvdsector, (char *) &pvd, &currentsector);
1141 both32((unsigned char *) pvd.pathtable, pathbytes);
1142 little32((unsigned char *) &pvd.first_little_pathtable_start, littlepath);
1143 little32((unsigned char *) &pvd.first_big_pathtable_start, bigpath);
1145 /* write root dir entry in pvd */
1146 seeksector(fd, pvdsector, &currentsector);
1147 Lseek(fd, (int)((char *) &pvd.rootrecord - (char *) &pvd), SEEK_CUR);
1148 if(write_direntry(CURRENTDIR, root.startsector, root.bytesize,
1149 root.isdir, fd) > sizeof(pvd.rootrecord)) {
1150 fprintf(stderr, "warning: unexpectedly large root record\n");
1153 if(bootimage) {
1154 fprintf(stderr, " * rewriting boot catalog\n");
1155 seeksector(fd, bootcatalogsector, &currentsector);
1156 writebootcatalog(fd, &currentsector, imagesector, imagesectors);
1158 /* finish and rewrite the boot record volume descriptor */
1159 fprintf(stderr, " * rewriting the boot rvd\n");
1160 seeksector(fd, bootvolumesector, &currentsector);
1161 writebootrecord(fd, &currentsector, bootcatalogsector);
1163 if(appendsectorinfo) {
1164 Lseek(bootfd, 0, SEEK_SET);
1165 fprintf(stderr, " * rewriting boot image\n");
1166 seeksector(fd, imagesector, &currentsector);
1167 writebootimage(bootimage, bootfd,
1168 fd, &currentsector, appendsectorinfo, &root);
1170 close(bootfd);
1174 fprintf(stderr, " * all ok\n");
1176 return 0;
1179 int get_system_type(int fd)
1181 off_t old_pos;
1182 size_t size;
1183 ssize_t r;
1184 int type;
1185 struct part_entry *partp;
1186 unsigned char bootsector[512];
1188 errno= 0;
1189 old_pos= lseek(fd, 0, SEEK_SET);
1190 if (old_pos == -1 && errno != 0)
1192 fprintf(stderr, "bootimage file is not seekable: %s\n",
1193 strerror(errno));
1194 exit(1);
1196 size= sizeof(bootsector);
1197 r= read(fd, bootsector, size);
1198 if (r != size)
1200 fprintf(stderr, "error reading bootimage file: %s\n",
1201 r < 0 ? strerror(errno) : "unexpected EOF");
1202 exit(1);
1204 if (bootsector[size-2] != 0x55 && bootsector[size-1] != 0xAA)
1206 fprintf(stderr, "bad magic in bootimage file\n");
1207 exit(1);
1210 partp= (struct part_entry *)&bootsector[PART_TABLE_OFF];
1211 type= partp->sysind;
1212 if (type == NO_PART)
1214 fprintf(stderr, "first partition table entry is unused\n");
1215 exit(1);
1217 if (!(partp->bootind & ACTIVE_FLAG))
1219 fprintf(stderr, "first partition table entry is not active\n");
1220 exit(1);
1223 lseek(fd, old_pos, SEEK_SET);
1224 return type;