xref: /minix3/minix/commands/writeisofs/writeisofs.c (revision 7c48de6cc4c6d56f2277d378dba01dbac8a8c3b9)
1433d6423SLionel Sambuc 
2433d6423SLionel Sambuc /* writeisofs - simple ISO9660-format-image writing utility */
3433d6423SLionel Sambuc 
4433d6423SLionel Sambuc #if HAVE_NBTOOL_CONFIG_H
5433d6423SLionel Sambuc #include "nbtool_config.h"
6433d6423SLionel Sambuc #endif
7433d6423SLionel Sambuc 
8433d6423SLionel Sambuc #include <errno.h>
9433d6423SLionel Sambuc #include <stdio.h>
10433d6423SLionel Sambuc #include <time.h>
11433d6423SLionel Sambuc #include <stdlib.h>
12433d6423SLionel Sambuc #include <fcntl.h>
13433d6423SLionel Sambuc #include <string.h>
14433d6423SLionel Sambuc #include <unistd.h>
15433d6423SLionel Sambuc #include <dirent.h>
16433d6423SLionel Sambuc #include <assert.h>
17433d6423SLionel Sambuc #include <ctype.h>
18433d6423SLionel Sambuc #include <partition.h>
19*7c48de6cSDavid van Moolenbroek #include <inttypes.h>
20433d6423SLionel Sambuc 
21433d6423SLionel Sambuc #include <sys/stat.h>
22433d6423SLionel Sambuc 
23433d6423SLionel Sambuc #define Writefield(fd, f) Write(fd, &(f), sizeof(f))
24433d6423SLionel Sambuc 
25433d6423SLionel Sambuc extern char *optarg;
26433d6423SLionel Sambuc extern int optind;
27433d6423SLionel Sambuc 
28433d6423SLionel Sambuc #ifndef min
29433d6423SLionel Sambuc #define min(a,b) ((a) < (b) ? (a) : (b))
30433d6423SLionel Sambuc #endif
31433d6423SLionel Sambuc 
32433d6423SLionel Sambuc #define FLAG_DIR	2
33433d6423SLionel Sambuc 
34433d6423SLionel Sambuc #include <sys/types.h>
35433d6423SLionel Sambuc #include <sys/stat.h>
36433d6423SLionel Sambuc 
37433d6423SLionel Sambuc #define NAMELEN		500
38433d6423SLionel Sambuc #define ISONAMELEN	12	/* XXX could easily be 31 */
39433d6423SLionel Sambuc #define PLATFORM_80X86	0
40433d6423SLionel Sambuc 
41433d6423SLionel Sambuc #define ISO_SECTOR 2048
42433d6423SLionel Sambuc #define VIRTUAL_SECTOR	512
43433d6423SLionel Sambuc #define ROUNDUP(v, n) (((v)+(n)-1)/(n))
44433d6423SLionel Sambuc 
45433d6423SLionel Sambuc #define CURRENTDIR	"."
46433d6423SLionel Sambuc #define PARENTDIR	".."
47433d6423SLionel Sambuc 
48433d6423SLionel Sambuc /*  *** CD (disk) data structures ********************* */
49433d6423SLionel Sambuc 
50433d6423SLionel Sambuc /* primary volume descriptor */
51433d6423SLionel Sambuc 
52433d6423SLionel Sambuc struct pvd {
53433d6423SLionel Sambuc 	u_int8_t one;
54433d6423SLionel Sambuc 	char set[6];
55433d6423SLionel Sambuc 	u_int8_t zero;
56433d6423SLionel Sambuc 	char system[32];
57433d6423SLionel Sambuc 	char volume[32];
58433d6423SLionel Sambuc 	u_int8_t zeroes1[8];
59433d6423SLionel Sambuc 	u_int32_t sectors[2];
60433d6423SLionel Sambuc 	u_int8_t zeroes2[32];
61433d6423SLionel Sambuc 	u_int16_t setsize[2];
62433d6423SLionel Sambuc 	u_int16_t seq[2];
63433d6423SLionel Sambuc 	u_int16_t sectorsize[2];
64433d6423SLionel Sambuc 	u_int32_t pathtablesize[2];
65433d6423SLionel Sambuc 	u_int32_t first_little_pathtable_start;
66433d6423SLionel Sambuc 	u_int32_t second_little_pathtable_start;
67433d6423SLionel Sambuc 	u_int32_t first_big_pathtable_start;
68433d6423SLionel Sambuc 	u_int32_t second_big_pathtable_start;
69433d6423SLionel Sambuc 	u_int8_t rootrecord[34];
70433d6423SLionel Sambuc 	u_int8_t volumeset[128];
71433d6423SLionel Sambuc 	u_int8_t publisher[128];
72433d6423SLionel Sambuc 	u_int8_t preparer[128];
73433d6423SLionel Sambuc 	u_int8_t application[128];
74433d6423SLionel Sambuc 	u_int8_t copyrightfile[37];
75433d6423SLionel Sambuc 	u_int8_t abstractfile[37];
76433d6423SLionel Sambuc 	u_int8_t bibliofile[37];
77433d6423SLionel Sambuc 	u_int8_t create[17];
78433d6423SLionel Sambuc 	u_int8_t modified[17];
79433d6423SLionel Sambuc 	char expiry[17];
80433d6423SLionel Sambuc 	u_int8_t effective[17];
81433d6423SLionel Sambuc 	u_int8_t one2;
82433d6423SLionel Sambuc 	u_int8_t zero2;
83433d6423SLionel Sambuc 	u_int8_t zeroes3[512];
84433d6423SLionel Sambuc 	u_int8_t zeroes4[653];
85433d6423SLionel Sambuc };
86433d6423SLionel Sambuc 
87433d6423SLionel Sambuc /* boot record volume descriptor */
88433d6423SLionel Sambuc 
89433d6423SLionel Sambuc struct bootrecord {
90433d6423SLionel Sambuc 	u_int8_t	indicator;	/* 0 */
91433d6423SLionel Sambuc 	char		set[5];		/* "CD001" */
92433d6423SLionel Sambuc 	u_int8_t	version;	/* 1 */
93433d6423SLionel Sambuc 	char		ident[32];	/* "EL TORITO SPECIFICATION" */
94433d6423SLionel Sambuc 	u_int8_t	zero[32];	/* unused, must be 0 */
95433d6423SLionel Sambuc 	u_int32_t	bootcatalog;	/* starting sector of boot catalog */
96433d6423SLionel Sambuc 	u_int8_t	zero2[1973];	/* unused, must be 0 */
97433d6423SLionel Sambuc };
98433d6423SLionel Sambuc 
99433d6423SLionel Sambuc /* boot catalog validation entry */
100433d6423SLionel Sambuc 
101433d6423SLionel Sambuc struct bc_validation {
102433d6423SLionel Sambuc 	u_int8_t	headerid;	/* 1 */
103433d6423SLionel Sambuc 	u_int8_t	platform;	/* 0: 80x86; 1: powerpc; 2: mac */
104433d6423SLionel Sambuc 	u_int8_t	zero[2];	/* unused, must be 0 */
105433d6423SLionel Sambuc 	char		idstring[24];	/* id string */
106433d6423SLionel Sambuc 	u_int16_t	checksum;
107433d6423SLionel Sambuc 	u_int8_t	keys[2];	/* 0x55AA */
108433d6423SLionel Sambuc };
109433d6423SLionel Sambuc 
110433d6423SLionel Sambuc /* boot catalog initial/default entry */
111433d6423SLionel Sambuc 
112433d6423SLionel Sambuc #define INDICATE_BOOTABLE	0x88
113433d6423SLionel Sambuc 
114433d6423SLionel Sambuc #define BOOTMEDIA_UNSPECIFIED	-1
115433d6423SLionel Sambuc #define BOOTMEDIA_NONE		0
116433d6423SLionel Sambuc #define BOOTMEDIA_1200K		1
117433d6423SLionel Sambuc #define BOOTMEDIA_1440K		2
118433d6423SLionel Sambuc #define BOOTMEDIA_2880K		3
119433d6423SLionel Sambuc #define BOOTMEDIA_HARDDISK	4
120433d6423SLionel Sambuc 
121433d6423SLionel Sambuc struct bc_initial {
122433d6423SLionel Sambuc 	u_int8_t	indicator;	/* INDICATE_BOOTABLE */
123433d6423SLionel Sambuc 	u_int8_t	media;		/* BOOTMEDIA_* */
124433d6423SLionel Sambuc 	u_int16_t	seg;		/* load segment or 0 for default */
125433d6423SLionel Sambuc 	u_int8_t	type;		/* system type (from part. table) */
126433d6423SLionel Sambuc 	u_int8_t	zero;
127433d6423SLionel Sambuc 	u_int16_t	sectors;
128433d6423SLionel Sambuc 	u_int32_t	startsector;
129433d6423SLionel Sambuc 	u_int8_t	zero2[20];
130433d6423SLionel Sambuc };
131433d6423SLionel Sambuc 
132433d6423SLionel Sambuc /* directory entry */
133433d6423SLionel Sambuc 
134433d6423SLionel Sambuc struct dir {
135433d6423SLionel Sambuc 	u_int8_t	recordsize;
136433d6423SLionel Sambuc 	u_int8_t	extended;
137433d6423SLionel Sambuc 	u_int32_t	datasector[2];
138433d6423SLionel Sambuc 	u_int32_t	filesize[2];
139433d6423SLionel Sambuc 	u_int8_t	year;
140433d6423SLionel Sambuc 	u_int8_t	month;
141433d6423SLionel Sambuc 	u_int8_t	day;
142433d6423SLionel Sambuc 	u_int8_t	hour;
143433d6423SLionel Sambuc 	u_int8_t	minute;
144433d6423SLionel Sambuc 	u_int8_t	second;
145433d6423SLionel Sambuc 	u_int8_t	offset;
146433d6423SLionel Sambuc 	u_int8_t	flags;
147433d6423SLionel Sambuc 	u_int8_t	interleaved;
148433d6423SLionel Sambuc 	u_int8_t	interleavegap;
149433d6423SLionel Sambuc 	u_int16_t	sequence[2];
150433d6423SLionel Sambuc 	u_int8_t	namelen;
151433d6423SLionel Sambuc 	char		name[NAMELEN];
152433d6423SLionel Sambuc };
153433d6423SLionel Sambuc 
154433d6423SLionel Sambuc /*  *** program (memory) data structures ********************* */
155433d6423SLionel Sambuc 
156433d6423SLionel Sambuc struct node {
157433d6423SLionel Sambuc 	char name[NAMELEN];
158433d6423SLionel Sambuc 	time_t timestamp;
159433d6423SLionel Sambuc 	int isdir;
160433d6423SLionel Sambuc 	int pathtablerecord;
161433d6423SLionel Sambuc 	struct node *firstchild, *nextchild;
162433d6423SLionel Sambuc 
163433d6423SLionel Sambuc 	/* filled out at i/o time */
164433d6423SLionel Sambuc 	u_int32_t startsector, bytesize;
165433d6423SLionel Sambuc };
166433d6423SLionel Sambuc 
167433d6423SLionel Sambuc int n_reserved_pathtableentries = 0, n_used_pathtableentries = 0;
168433d6423SLionel Sambuc int bootmedia = BOOTMEDIA_UNSPECIFIED;
169433d6423SLionel Sambuc unsigned long bootseg = 0;
170433d6423SLionel Sambuc int system_type = 0;
171433d6423SLionel Sambuc 
172433d6423SLionel Sambuc int get_system_type(int fd);
173433d6423SLionel Sambuc 
174433d6423SLionel Sambuc ssize_t
Write(int fd,void * buf,ssize_t len)175433d6423SLionel Sambuc Write(int fd, void *buf, ssize_t len)
176433d6423SLionel Sambuc {
177433d6423SLionel Sambuc 	ssize_t r;
178433d6423SLionel Sambuc 	if((r=write(fd, buf, len)) != len) {
179433d6423SLionel Sambuc 		if(r < 0) { perror("write"); }
180433d6423SLionel Sambuc 		fprintf(stderr, "failed or short write - aborting.\n");
181433d6423SLionel Sambuc 		exit(1);
182433d6423SLionel Sambuc 	}
183433d6423SLionel Sambuc 	return len;
184433d6423SLionel Sambuc }
185433d6423SLionel Sambuc 
186433d6423SLionel Sambuc off_t
Lseek(int fd,off_t pos,int rel)187433d6423SLionel Sambuc Lseek(int fd, off_t pos, int rel)
188433d6423SLionel Sambuc {
189433d6423SLionel Sambuc 	off_t r;
190433d6423SLionel Sambuc 
191433d6423SLionel Sambuc 	if((r=lseek(fd, pos, rel)) < 0) {
192433d6423SLionel Sambuc 		perror("lseek");
193433d6423SLionel Sambuc 		fprintf(stderr, "lseek failed - aborting.\n");
194433d6423SLionel Sambuc 		exit(1);
195433d6423SLionel Sambuc 	}
196433d6423SLionel Sambuc 
197433d6423SLionel Sambuc 	return r;
198433d6423SLionel Sambuc }
199433d6423SLionel Sambuc 
200433d6423SLionel Sambuc void
writesector(int fd,char * block,int * currentsector)201433d6423SLionel Sambuc writesector(int fd, char *block, int *currentsector)
202433d6423SLionel Sambuc {
203433d6423SLionel Sambuc 	Write(fd, block, ISO_SECTOR);
204433d6423SLionel Sambuc 	(*currentsector)++;
205433d6423SLionel Sambuc 	return;
206433d6423SLionel Sambuc }
207433d6423SLionel Sambuc 
208433d6423SLionel Sambuc void
seeksector(int fd,int sector,int * currentsector)209433d6423SLionel Sambuc seeksector(int fd, int sector, int *currentsector)
210433d6423SLionel Sambuc {
211433d6423SLionel Sambuc 	Lseek(fd, sector*ISO_SECTOR, SEEK_SET);
212433d6423SLionel Sambuc 	*currentsector = sector;
213433d6423SLionel Sambuc }
214433d6423SLionel Sambuc 
215433d6423SLionel Sambuc void
seekwritesector(int fd,int sector,char * block,int * currentsector)216433d6423SLionel Sambuc seekwritesector(int fd, int sector, char *block, int *currentsector)
217433d6423SLionel Sambuc {
218433d6423SLionel Sambuc 	seeksector(fd, sector, currentsector);
219433d6423SLionel Sambuc 	writesector(fd, block, currentsector);
220433d6423SLionel Sambuc }
221433d6423SLionel Sambuc 
222433d6423SLionel Sambuc ssize_t
Read(int fd,void * buf,ssize_t len)223433d6423SLionel Sambuc Read(int fd, void *buf, ssize_t len)
224433d6423SLionel Sambuc {
225433d6423SLionel Sambuc 	ssize_t r;
226433d6423SLionel Sambuc 	if((r=read(fd, buf, len)) != len) {
227433d6423SLionel Sambuc 		if(r < 0) { perror("read"); }
228433d6423SLionel Sambuc 		fprintf(stderr, "failed or short read.\n");
229433d6423SLionel Sambuc 		exit(1);
230433d6423SLionel Sambuc 	}
231433d6423SLionel Sambuc 
232433d6423SLionel Sambuc 	return len;
233433d6423SLionel Sambuc }
234433d6423SLionel Sambuc 
both16(unsigned char * both,unsigned short i16)235433d6423SLionel Sambuc void both16(unsigned char *both, unsigned short i16)
236433d6423SLionel Sambuc {
237433d6423SLionel Sambuc 	unsigned char *little, *big;
238433d6423SLionel Sambuc 
239433d6423SLionel Sambuc 	little = both;
240433d6423SLionel Sambuc 	big = both + 2;
241433d6423SLionel Sambuc 
242433d6423SLionel Sambuc 	little[0] = big[1] = i16        & 0xFF;
243433d6423SLionel Sambuc 	little[1] = big[0] = (i16 >> 8) & 0xFF;
244433d6423SLionel Sambuc }
245433d6423SLionel Sambuc 
both32(unsigned char * both,unsigned long i32)246433d6423SLionel Sambuc void both32(unsigned char *both, unsigned long i32)
247433d6423SLionel Sambuc {
248433d6423SLionel Sambuc 	unsigned char *little, *big;
249433d6423SLionel Sambuc 
250433d6423SLionel Sambuc 	little = both;
251433d6423SLionel Sambuc 	big = both + 4;
252433d6423SLionel Sambuc 
253433d6423SLionel Sambuc 	little[0] = big[3] = i32         & 0xFF;
254433d6423SLionel Sambuc 	little[1] = big[2] = (i32 >>  8) & 0xFF;
255433d6423SLionel Sambuc 	little[2] = big[1] = (i32 >> 16) & 0xFF;
256433d6423SLionel Sambuc 	little[3] = big[0] = (i32 >> 24) & 0xFF;
257433d6423SLionel Sambuc }
258433d6423SLionel Sambuc 
259433d6423SLionel Sambuc #define MINDIRLEN	 1
260433d6423SLionel Sambuc #define MAXDIRLEN	31
261433d6423SLionel Sambuc 
262433d6423SLionel Sambuc #define MAXLEVEL 8
263433d6423SLionel Sambuc 
cmpf(const void * v1,const void * v2)264433d6423SLionel Sambuc static int cmpf(const void *v1, const void *v2)
265433d6423SLionel Sambuc {
266433d6423SLionel Sambuc 	struct node *n1, *n2;
267433d6423SLionel Sambuc 	int i;
268433d6423SLionel Sambuc 	char f1[NAMELEN], f2[NAMELEN];
269433d6423SLionel Sambuc 
270433d6423SLionel Sambuc 	n1 = (struct node *) v1;
271433d6423SLionel Sambuc 	n2 = (struct node *) v2;
272433d6423SLionel Sambuc 	strcpy(f1, n1->name);
273433d6423SLionel Sambuc 	strcpy(f2, n2->name);
274433d6423SLionel Sambuc 	for(i = 0; i < strlen(f1); i++) f1[i] = toupper(f1[i]);
275433d6423SLionel Sambuc 	for(i = 0; i < strlen(f2); i++) f2[i] = toupper(f2[i]);
276433d6423SLionel Sambuc 
277433d6423SLionel Sambuc 
278433d6423SLionel Sambuc 	return -strcmp(f1, f2);
279433d6423SLionel Sambuc }
280433d6423SLionel Sambuc 
281433d6423SLionel Sambuc void
maketree(struct node * thisdir,char * name,int level)282433d6423SLionel Sambuc maketree(struct node *thisdir, char *name, int level)
283433d6423SLionel Sambuc {
284433d6423SLionel Sambuc 	DIR *dir;
285433d6423SLionel Sambuc 	struct dirent *e;
286433d6423SLionel Sambuc 	struct node *dirnodes = NULL;
287433d6423SLionel Sambuc 	int reserved_dirnodes = 0, used_dirnodes = 0;
288433d6423SLionel Sambuc 	struct node *child;
289433d6423SLionel Sambuc 
290433d6423SLionel Sambuc 	thisdir->firstchild = NULL;
291433d6423SLionel Sambuc 	thisdir->isdir = 1;
292433d6423SLionel Sambuc 	thisdir->startsector = 0xdeadbeef;
293433d6423SLionel Sambuc 
294433d6423SLionel Sambuc 	if(level >= MAXLEVEL) {
295433d6423SLionel Sambuc 		fprintf(stderr, "ignoring entries in %s (too deep for iso9660)\n",
296433d6423SLionel Sambuc 			name);
297433d6423SLionel Sambuc 		return;
298433d6423SLionel Sambuc 	}
299433d6423SLionel Sambuc 
300433d6423SLionel Sambuc 	if(!(dir = opendir(CURRENTDIR))) {
301433d6423SLionel Sambuc 		perror("opendir");
302433d6423SLionel Sambuc 		return;
303433d6423SLionel Sambuc 	}
304433d6423SLionel Sambuc 
305433d6423SLionel Sambuc 	/* how many entries do we need to allocate? */
306433d6423SLionel Sambuc 	while(readdir(dir)) reserved_dirnodes++;
307433d6423SLionel Sambuc 	if(!reserved_dirnodes) {
308433d6423SLionel Sambuc 		closedir(dir);
309433d6423SLionel Sambuc 		return;
310433d6423SLionel Sambuc 	}
311433d6423SLionel Sambuc 
312433d6423SLionel Sambuc 	if(!(dirnodes = malloc(sizeof(*dirnodes)*reserved_dirnodes))) {
313*7c48de6cSDavid van Moolenbroek 		fprintf(stderr, "couldn't allocate dirnodes (%zu bytes)\n",
314433d6423SLionel Sambuc 			sizeof(*dirnodes)*reserved_dirnodes);
315433d6423SLionel Sambuc 		exit(1);
316433d6423SLionel Sambuc 	}
317433d6423SLionel Sambuc 
318433d6423SLionel Sambuc 
319433d6423SLionel Sambuc 	/* remember all entries in this dir */
320433d6423SLionel Sambuc 	rewinddir(dir);
321433d6423SLionel Sambuc 
322433d6423SLionel Sambuc 	child = dirnodes;
323433d6423SLionel Sambuc 	while((e=readdir(dir))) {
324433d6423SLionel Sambuc 		struct stat st;
325433d6423SLionel Sambuc 		mode_t type;
326433d6423SLionel Sambuc 		if(!strcmp(e->d_name, CURRENTDIR) || !strcmp(e->d_name, PARENTDIR))
327433d6423SLionel Sambuc 			continue;
328433d6423SLionel Sambuc 		if(stat(e->d_name, &st) < 0) {
329433d6423SLionel Sambuc 			perror(e->d_name);
330433d6423SLionel Sambuc 			fprintf(stderr, "failed to stat file/dir\n");
331433d6423SLionel Sambuc 			exit(1);
332433d6423SLionel Sambuc 		}
333433d6423SLionel Sambuc 
334433d6423SLionel Sambuc 		type = st.st_mode & S_IFMT;
335433d6423SLionel Sambuc 
336433d6423SLionel Sambuc /*
337433d6423SLionel Sambuc 		printf("%s type: %x dir: %x file: %x\n",
338433d6423SLionel Sambuc 			e->d_name, type, S_IFDIR, S_IFREG);
339433d6423SLionel Sambuc 			*/
340433d6423SLionel Sambuc 		if(type != S_IFDIR && type != S_IFREG)
341433d6423SLionel Sambuc 			continue;
342433d6423SLionel Sambuc 
343433d6423SLionel Sambuc 		used_dirnodes++;
344433d6423SLionel Sambuc 		if(used_dirnodes > reserved_dirnodes) {
345433d6423SLionel Sambuc 			fprintf(stderr, "huh, directory entries appeared "
346433d6423SLionel Sambuc 	"(not enough pre-allocated nodes; this can't happen) ?\n");
347433d6423SLionel Sambuc 			exit(1);
348433d6423SLionel Sambuc 		}
349433d6423SLionel Sambuc 
350433d6423SLionel Sambuc 		if(type == S_IFDIR) {
351433d6423SLionel Sambuc 			child->isdir = 1;
352433d6423SLionel Sambuc 		} else {
353433d6423SLionel Sambuc 			child->isdir = 0;
354433d6423SLionel Sambuc 			child->firstchild = NULL;
355433d6423SLionel Sambuc 		}
356433d6423SLionel Sambuc 		strlcpy(child->name, e->d_name, sizeof(child->name));
357433d6423SLionel Sambuc 		child->timestamp = st.st_mtime;
358433d6423SLionel Sambuc 
359433d6423SLionel Sambuc 		child++;
360433d6423SLionel Sambuc 	}
361433d6423SLionel Sambuc 
362433d6423SLionel Sambuc 	closedir(dir);
363433d6423SLionel Sambuc 
364433d6423SLionel Sambuc 	if(!used_dirnodes)
365433d6423SLionel Sambuc 		return;
366433d6423SLionel Sambuc 
367433d6423SLionel Sambuc 	if(!(dirnodes=realloc(dirnodes, used_dirnodes*sizeof(*dirnodes)))) {
368433d6423SLionel Sambuc 		fprintf(stderr, "realloc() of dirnodes failed - aborting\n");
369433d6423SLionel Sambuc 		exit(1);
370433d6423SLionel Sambuc 	}
371433d6423SLionel Sambuc 
372433d6423SLionel Sambuc 	qsort(dirnodes, used_dirnodes, sizeof(*dirnodes), cmpf);
373433d6423SLionel Sambuc 
374433d6423SLionel Sambuc 	child = dirnodes;
375433d6423SLionel Sambuc 
376433d6423SLionel Sambuc 	while(used_dirnodes--) {
377433d6423SLionel Sambuc 		child->nextchild = thisdir->firstchild;
378433d6423SLionel Sambuc 		thisdir->firstchild = child;
379433d6423SLionel Sambuc 		if(child->isdir) {
380433d6423SLionel Sambuc 			if(chdir(child->name) < 0) {
381433d6423SLionel Sambuc 				perror(child->name);
382433d6423SLionel Sambuc 			} else {
383433d6423SLionel Sambuc 				maketree(child, child->name, level+1);
384433d6423SLionel Sambuc 				if(chdir(PARENTDIR) < 0) {
385433d6423SLionel Sambuc 					perror("chdir() failed");
386433d6423SLionel Sambuc 					fprintf(stderr, "couldn't chdir() to parent, aborting\n");
387433d6423SLionel Sambuc 					exit(1);
388433d6423SLionel Sambuc 				}
389433d6423SLionel Sambuc 			}
390433d6423SLionel Sambuc 		}
391433d6423SLionel Sambuc 
392433d6423SLionel Sambuc 		child++;
393433d6423SLionel Sambuc 	}
394433d6423SLionel Sambuc 
395433d6423SLionel Sambuc }
396433d6423SLionel Sambuc 
397433d6423SLionel Sambuc void
little32(unsigned char * dest,u_int32_t src)398433d6423SLionel Sambuc little32(unsigned char *dest, u_int32_t src)
399433d6423SLionel Sambuc {
400433d6423SLionel Sambuc 	dest[0] = ((src >>  0) & 0xFF);
401433d6423SLionel Sambuc 	dest[1] = ((src >>  8) & 0xFF);
402433d6423SLionel Sambuc 	dest[2] = ((src >> 16) & 0xFF);
403433d6423SLionel Sambuc 	dest[3] = ((src >> 24) & 0xFF);
404433d6423SLionel Sambuc 
405433d6423SLionel Sambuc 	return;
406433d6423SLionel Sambuc }
407433d6423SLionel Sambuc 
408433d6423SLionel Sambuc void
little16(unsigned char * dest,u_int16_t src)409433d6423SLionel Sambuc little16(unsigned char *dest, u_int16_t src)
410433d6423SLionel Sambuc {
411433d6423SLionel Sambuc 	dest[0] = ((src >>  0) & 0xFF);
412433d6423SLionel Sambuc 	dest[1] = ((src >>  8) & 0xFF);
413433d6423SLionel Sambuc 
414433d6423SLionel Sambuc 	return;
415433d6423SLionel Sambuc }
416433d6423SLionel Sambuc 
417433d6423SLionel Sambuc void
big32(unsigned char * dest,u_int32_t src)418433d6423SLionel Sambuc big32(unsigned char *dest, u_int32_t src)
419433d6423SLionel Sambuc {
420433d6423SLionel Sambuc 	dest[3] = ((src >>  0) & 0xFF);
421433d6423SLionel Sambuc 	dest[2] = ((src >>  8) & 0xFF);
422433d6423SLionel Sambuc 	dest[1] = ((src >> 16) & 0xFF);
423433d6423SLionel Sambuc 	dest[0] = ((src >> 24) & 0xFF);
424433d6423SLionel Sambuc 	return;
425433d6423SLionel Sambuc }
426433d6423SLionel Sambuc 
427433d6423SLionel Sambuc void
big16(unsigned char * dest,u_int16_t src)428433d6423SLionel Sambuc big16(unsigned char *dest, u_int16_t src)
429433d6423SLionel Sambuc {
430433d6423SLionel Sambuc 	dest[1] = ((src >>  0) & 0xFF);
431433d6423SLionel Sambuc 	dest[0] = ((src >>  8) & 0xFF);
432433d6423SLionel Sambuc 	return;
433433d6423SLionel Sambuc }
434433d6423SLionel Sambuc 
435433d6423SLionel Sambuc 
436433d6423SLionel Sambuc void
traversetree(struct node * root,int level,int littleendian,int maxlevel,int * bytes,int fd,int parentrecord,int * recordno)437433d6423SLionel Sambuc traversetree(struct node *root, int level, int littleendian,
438433d6423SLionel Sambuc 	int maxlevel, int *bytes, int fd, int parentrecord, int *recordno)
439433d6423SLionel Sambuc {
440433d6423SLionel Sambuc 	struct node *child;
441433d6423SLionel Sambuc 	struct pte {
442433d6423SLionel Sambuc 		u_int8_t len;
443433d6423SLionel Sambuc 		u_int8_t zero;
444433d6423SLionel Sambuc 		u_int32_t startsector;
445433d6423SLionel Sambuc 		u_int16_t parent;
446433d6423SLionel Sambuc 	} pte;
447433d6423SLionel Sambuc 
448433d6423SLionel Sambuc 	if(level == maxlevel) {
449433d6423SLionel Sambuc 		int i;
450433d6423SLionel Sambuc 		char newname[NAMELEN];
451433d6423SLionel Sambuc 		if(!root->isdir)
452433d6423SLionel Sambuc 			return;
453433d6423SLionel Sambuc 		pte.zero = 0;
454433d6423SLionel Sambuc 		if(level == 1) {
455433d6423SLionel Sambuc 			/* root */
456433d6423SLionel Sambuc 			pte.len = 1;
457433d6423SLionel Sambuc 			pte.parent = 1;
458433d6423SLionel Sambuc 			root->name[0] = root->name[1] = '\0';
459433d6423SLionel Sambuc 		} else {
460433d6423SLionel Sambuc 			pte.len = strlen(root->name);
461433d6423SLionel Sambuc 			pte.parent = parentrecord;
462433d6423SLionel Sambuc 		}
463433d6423SLionel Sambuc 		pte.startsector = root->startsector;
464433d6423SLionel Sambuc 		root->pathtablerecord = (*recordno)++;
465433d6423SLionel Sambuc 
466433d6423SLionel Sambuc 		if(littleendian) {
467433d6423SLionel Sambuc 			little32((unsigned char *) &pte.startsector, pte.startsector);
468433d6423SLionel Sambuc 			little16((unsigned char *) &pte.parent, pte.parent);
469433d6423SLionel Sambuc 		} else {
470433d6423SLionel Sambuc 			big32((unsigned char *) &pte.startsector, pte.startsector);
471433d6423SLionel Sambuc 			big16((unsigned char *) &pte.parent, pte.parent);
472433d6423SLionel Sambuc 		}
473433d6423SLionel Sambuc 
474433d6423SLionel Sambuc 		*bytes += Write(fd, &pte.len, sizeof(pte.len));
475433d6423SLionel Sambuc 		*bytes += Write(fd, &pte.zero, sizeof(pte.zero));
476433d6423SLionel Sambuc 		*bytes += Write(fd, &pte.startsector, sizeof(pte.startsector));
477433d6423SLionel Sambuc 		*bytes += Write(fd, &pte.parent, sizeof(pte.parent));
478433d6423SLionel Sambuc 		if(!(pte.len%2))
479433d6423SLionel Sambuc 			root->name[pte.len++] = '\0';
480433d6423SLionel Sambuc 		for(i = 0; i < pte.len; i++)
481433d6423SLionel Sambuc 			newname[i] = toupper(root->name[i]);
482433d6423SLionel Sambuc 		*bytes += Write(fd, newname, pte.len);
483433d6423SLionel Sambuc 		return;
484433d6423SLionel Sambuc 	}
485433d6423SLionel Sambuc 
486433d6423SLionel Sambuc 	for(child = root->firstchild; child; child = child->nextchild)
487433d6423SLionel Sambuc 		if(child->isdir)
488433d6423SLionel Sambuc 			traversetree(child, level+1, littleendian,
489433d6423SLionel Sambuc 				maxlevel, bytes, fd, root->pathtablerecord,
490433d6423SLionel Sambuc 					recordno);
491433d6423SLionel Sambuc 
492433d6423SLionel Sambuc 	return;
493433d6423SLionel Sambuc }
494433d6423SLionel Sambuc 
495433d6423SLionel Sambuc int
makepathtables(struct node * root,int littleendian,int * bytes,int fd)496433d6423SLionel Sambuc makepathtables(struct node *root, int littleendian, int *bytes, int fd)
497433d6423SLionel Sambuc {
498433d6423SLionel Sambuc 	int level;
499433d6423SLionel Sambuc 	static char block[ISO_SECTOR];
500433d6423SLionel Sambuc 	int recordno;
501433d6423SLionel Sambuc 
502433d6423SLionel Sambuc 	recordno = 1;
503433d6423SLionel Sambuc 
504433d6423SLionel Sambuc 	*bytes = 0;
505433d6423SLionel Sambuc 
506433d6423SLionel Sambuc 	for(level = 1; level <= MAXLEVEL; level++)
507433d6423SLionel Sambuc 		traversetree(root, 1, littleendian, level, bytes, fd, 1, &recordno);
508433d6423SLionel Sambuc 
509433d6423SLionel Sambuc 	if(*bytes % ISO_SECTOR) {
510433d6423SLionel Sambuc 		ssize_t x;
511433d6423SLionel Sambuc 		x = ISO_SECTOR-(*bytes % ISO_SECTOR);
512*7c48de6cSDavid van Moolenbroek 		Write(fd, block, x);
513433d6423SLionel Sambuc 		*bytes += x;
514433d6423SLionel Sambuc 	}
515433d6423SLionel Sambuc 
516433d6423SLionel Sambuc 	return *bytes/ISO_SECTOR;
517433d6423SLionel Sambuc }
518433d6423SLionel Sambuc 
519433d6423SLionel Sambuc ssize_t
write_direntry(struct node * n,char * origname,int fd)520433d6423SLionel Sambuc write_direntry(struct node * n, char *origname, int fd)
521433d6423SLionel Sambuc {
522433d6423SLionel Sambuc 	int namelen, total = 0;
523433d6423SLionel Sambuc 	struct dir entry;
524433d6423SLionel Sambuc 	char copyname[NAMELEN];
525433d6423SLionel Sambuc 	struct tm tm;
526433d6423SLionel Sambuc 
527433d6423SLionel Sambuc 	memset(&entry, 0, sizeof(entry));
528433d6423SLionel Sambuc 
529433d6423SLionel Sambuc 	if(!strcmp(origname, CURRENTDIR)) {
530433d6423SLionel Sambuc 		entry.name[0] = '\000';
531433d6423SLionel Sambuc 		namelen = 1;
532433d6423SLionel Sambuc 	} else if(!strcmp(origname, PARENTDIR)) {
533433d6423SLionel Sambuc 		entry.name[0] = '\001';
534433d6423SLionel Sambuc 		namelen = 1;
535433d6423SLionel Sambuc 	} else {
536433d6423SLionel Sambuc 		int i;
537433d6423SLionel Sambuc 		strlcpy(copyname, origname, sizeof(copyname));
538433d6423SLionel Sambuc 		namelen = strlen(copyname);
539433d6423SLionel Sambuc 
540433d6423SLionel Sambuc 		/* XXX No check for 8+3 ? (DOS compatibility) */
541433d6423SLionel Sambuc 		assert(ISONAMELEN <= NAMELEN-2);
542433d6423SLionel Sambuc 		if(namelen > ISONAMELEN) {
543433d6423SLionel Sambuc 			fprintf(stderr, "%s: truncated, too long for iso9660\n", copyname);
544433d6423SLionel Sambuc 			namelen = ISONAMELEN;
545433d6423SLionel Sambuc 			copyname[namelen] = '\0';
546433d6423SLionel Sambuc 		}
547433d6423SLionel Sambuc 
548433d6423SLionel Sambuc 		strlcpy(entry.name, copyname, namelen+1);
549433d6423SLionel Sambuc 		for(i = 0; i < namelen; i++)
550433d6423SLionel Sambuc 			entry.name[i] = toupper(entry.name[i]);
551433d6423SLionel Sambuc 
552433d6423SLionel Sambuc 		/* padding byte + system field */
553433d6423SLionel Sambuc 		entry.name[namelen]   = '\0';
554433d6423SLionel Sambuc 		entry.name[namelen+1] = '\0';
555433d6423SLionel Sambuc 		entry.name[namelen+2] = '\0';
556433d6423SLionel Sambuc 	}
557433d6423SLionel Sambuc 	entry.namelen = namelen;	/* original length */
558433d6423SLionel Sambuc 	if(!(namelen%2)) namelen++;	/* length with padding byte */
559433d6423SLionel Sambuc 
560433d6423SLionel Sambuc 
561433d6423SLionel Sambuc 	/* XXX 2 extra bytes for 'system use'.. */
562433d6423SLionel Sambuc 	entry.recordsize = 33 + namelen;
563433d6423SLionel Sambuc 	both32((unsigned char *) entry.datasector, n->startsector);
564433d6423SLionel Sambuc 	both32((unsigned char *) entry.filesize, n->bytesize);
565433d6423SLionel Sambuc 
566433d6423SLionel Sambuc 	if(n->isdir) entry.flags = FLAG_DIR;
567433d6423SLionel Sambuc 
568433d6423SLionel Sambuc 	memcpy(&tm, gmtime(&n->timestamp), sizeof(tm));
569433d6423SLionel Sambuc 	entry.year = (unsigned)tm.tm_year > 255 ? 255 : tm.tm_year;
570433d6423SLionel Sambuc 	entry.month = tm.tm_mon + 1;
571433d6423SLionel Sambuc 	entry.day = tm.tm_mday;
572433d6423SLionel Sambuc 	entry.hour = tm.tm_hour;
573433d6423SLionel Sambuc 	entry.minute = tm.tm_min;
574433d6423SLionel Sambuc 	entry.second = tm.tm_sec;
575433d6423SLionel Sambuc 	entry.offset = 0;	/* Posix uses UTC timestamps! */
576433d6423SLionel Sambuc 
577433d6423SLionel Sambuc 	both16((unsigned char *) entry.sequence, 1);
578433d6423SLionel Sambuc 
579433d6423SLionel Sambuc 	 total = Write(fd, &entry.recordsize, sizeof(entry.recordsize));
580433d6423SLionel Sambuc 	 total += Write(fd, &entry.extended, sizeof(entry.extended));
581433d6423SLionel Sambuc 	 total += Write(fd, entry.datasector, sizeof(entry.datasector));
582433d6423SLionel Sambuc 	 total += Write(fd, entry.filesize, sizeof(entry.filesize));
583433d6423SLionel Sambuc 	 total += Write(fd, &entry.year, sizeof(entry.year));
584433d6423SLionel Sambuc 	 total += Write(fd, &entry.month, sizeof(entry.month));
585433d6423SLionel Sambuc 	 total += Write(fd, &entry.day, sizeof(entry.day));
586433d6423SLionel Sambuc 	 total += Write(fd, &entry.hour, sizeof(entry.hour));
587433d6423SLionel Sambuc 	 total += Write(fd, &entry.minute, sizeof(entry.minute));
588433d6423SLionel Sambuc 	 total += Write(fd, &entry.second, sizeof(entry.second));
589433d6423SLionel Sambuc 	 total += Write(fd, &entry.offset, sizeof(entry.offset));
590433d6423SLionel Sambuc 	 total += Write(fd, &entry.flags, sizeof(entry.flags));
591433d6423SLionel Sambuc 	 total += Write(fd, &entry.interleaved, sizeof(entry.interleaved));
592433d6423SLionel Sambuc 	 total += Write(fd, &entry.interleavegap, sizeof(entry.interleavegap));
593433d6423SLionel Sambuc 	 total += Write(fd, entry.sequence, sizeof(entry.sequence));
594433d6423SLionel Sambuc 	 total += Write(fd, &entry.namelen, sizeof(entry.namelen));
595433d6423SLionel Sambuc 	 total += Write(fd, entry.name, namelen);
596433d6423SLionel Sambuc 
597433d6423SLionel Sambuc 	if(total != entry.recordsize || (total % 2) != 0) {
598433d6423SLionel Sambuc 		printf("%2d, %2d!  ", total, entry.recordsize);
599*7c48de6cSDavid van Moolenbroek 		printf("%3d = %3zu - %2zu + %2d\n",
600433d6423SLionel Sambuc 		entry.recordsize, sizeof(entry), sizeof(entry.name), namelen);
601433d6423SLionel Sambuc 	}
602433d6423SLionel Sambuc 
603433d6423SLionel Sambuc 	return entry.recordsize;
604433d6423SLionel Sambuc }
605433d6423SLionel Sambuc 
606433d6423SLionel Sambuc void
writedata(struct node * parent,struct node * root,int fd,int * currentsector,int dirs,struct dir * rootentry,int rootsize,int remove_after)607433d6423SLionel Sambuc writedata(struct node *parent, struct node *root,
608433d6423SLionel Sambuc 	int fd, int *currentsector, int dirs, struct dir *rootentry,
609433d6423SLionel Sambuc 	int rootsize, int remove_after)
610433d6423SLionel Sambuc {
611433d6423SLionel Sambuc 	static char buf[1024*1024];
612433d6423SLionel Sambuc 	struct node *c;
613433d6423SLionel Sambuc 	ssize_t written = 0, rest;
614433d6423SLionel Sambuc 
615433d6423SLionel Sambuc 	for(c = root->firstchild; c; c = c->nextchild) {
616433d6423SLionel Sambuc 		if(c->isdir && chdir(c->name) < 0) {
617433d6423SLionel Sambuc 			perror(c->name);
618433d6423SLionel Sambuc 			fprintf(stderr, "couldn't chdir to %s - aborting\n",
619433d6423SLionel Sambuc 				c->name);
620433d6423SLionel Sambuc 			exit(1);
621433d6423SLionel Sambuc 		}
622433d6423SLionel Sambuc 		writedata(root, c, fd, currentsector, dirs, rootentry, rootsize, remove_after);
623433d6423SLionel Sambuc 		if(c->isdir && chdir(PARENTDIR) < 0) {
624433d6423SLionel Sambuc 			perror("chdir to ..");
625433d6423SLionel Sambuc 			fprintf(stderr, "couldn't chdir to parent - "
626433d6423SLionel Sambuc 				"aborting\n");
627433d6423SLionel Sambuc 			exit(1);
628433d6423SLionel Sambuc 		}
629433d6423SLionel Sambuc 	}
630433d6423SLionel Sambuc 
631433d6423SLionel Sambuc 	/* write nodes depth-first, down-top */
632433d6423SLionel Sambuc 
633433d6423SLionel Sambuc 	if(root->isdir && dirs) {
634433d6423SLionel Sambuc 		/* dir */
635433d6423SLionel Sambuc 		written = 0;
636433d6423SLionel Sambuc 		root->startsector = *currentsector;
637433d6423SLionel Sambuc 		written += write_direntry(root, CURRENTDIR, fd);
638433d6423SLionel Sambuc 		if(parent) {
639433d6423SLionel Sambuc 			written += write_direntry(parent, PARENTDIR, fd);
640433d6423SLionel Sambuc 		} else {
641433d6423SLionel Sambuc 			written += write_direntry(root, PARENTDIR, fd);
642433d6423SLionel Sambuc 		}
643433d6423SLionel Sambuc 		for(c = root->firstchild; c; c = c->nextchild) {
644433d6423SLionel Sambuc 			off_t cur1, cur2;
645433d6423SLionel Sambuc 			ssize_t written_before;
646433d6423SLionel Sambuc 			cur1 = Lseek(fd, 0, SEEK_CUR);
647433d6423SLionel Sambuc 			written_before = written;
648433d6423SLionel Sambuc 			written += write_direntry(c, c->name, fd);
649433d6423SLionel Sambuc 			cur2 = Lseek(fd, 0, SEEK_CUR);
650433d6423SLionel Sambuc 			if(cur1/ISO_SECTOR != (cur2-1)/ISO_SECTOR) {
651433d6423SLionel Sambuc 				/* passed a sector boundary, argh! */
652433d6423SLionel Sambuc 				Lseek(fd, cur1, SEEK_SET);
653433d6423SLionel Sambuc 				written = written_before;
654433d6423SLionel Sambuc 				rest=(ISO_SECTOR-(written % ISO_SECTOR));
655433d6423SLionel Sambuc 				memset(buf, 0, rest);
656433d6423SLionel Sambuc 				Write(fd, buf, rest);
657433d6423SLionel Sambuc 				written += rest;
658433d6423SLionel Sambuc 				written += write_direntry(c, c->name, fd);
659433d6423SLionel Sambuc 			}
660433d6423SLionel Sambuc 		}
661433d6423SLionel Sambuc 		root->bytesize = written;
662433d6423SLionel Sambuc 	} else if(!root->isdir && !dirs) {
663433d6423SLionel Sambuc 		/* file */
664433d6423SLionel Sambuc 		struct stat st;
665433d6423SLionel Sambuc 		ssize_t rem;
666433d6423SLionel Sambuc 		int filefd;
667433d6423SLionel Sambuc 
668433d6423SLionel Sambuc 		if(stat(root->name, &st) < 0) {
669433d6423SLionel Sambuc 			perror(root->name);
670433d6423SLionel Sambuc 			fprintf(stderr, "couldn't stat %s - aborting\n", root->name);
671433d6423SLionel Sambuc 			exit(1);
672433d6423SLionel Sambuc 		}
673433d6423SLionel Sambuc 
674433d6423SLionel Sambuc 		if((filefd = open(root->name, O_RDONLY)) < 0) {
675433d6423SLionel Sambuc 			perror(root->name);
676433d6423SLionel Sambuc 			fprintf(stderr, "couldn't open %s - aborting\n", root->name);
677433d6423SLionel Sambuc 			exit(1);
678433d6423SLionel Sambuc 		}
679433d6423SLionel Sambuc 
680433d6423SLionel Sambuc 		rem = st.st_size;
681433d6423SLionel Sambuc 
682433d6423SLionel Sambuc 		root->startsector = *currentsector;
683433d6423SLionel Sambuc 
684433d6423SLionel Sambuc 		while(rem > 0) {
685433d6423SLionel Sambuc 			ssize_t chunk;
686433d6423SLionel Sambuc 			chunk = min(sizeof(buf), rem);
687433d6423SLionel Sambuc 			Read(filefd, buf, chunk);
688433d6423SLionel Sambuc 			Write(fd, buf, chunk);
689433d6423SLionel Sambuc 			rem -= chunk;
690433d6423SLionel Sambuc 		}
691433d6423SLionel Sambuc 
692433d6423SLionel Sambuc 		close(filefd);
693433d6423SLionel Sambuc 
694433d6423SLionel Sambuc 		root->bytesize = written = st.st_size;
695433d6423SLionel Sambuc 		if(remove_after && unlink(root->name) < 0) {
696433d6423SLionel Sambuc 			perror("unlink");
697433d6423SLionel Sambuc 			fprintf(stderr, "couldn't remove %s\n", root->name);
698433d6423SLionel Sambuc 		}
699433d6423SLionel Sambuc 	} else {
700433d6423SLionel Sambuc 		/* nothing to be done */
701433d6423SLionel Sambuc 		return;
702433d6423SLionel Sambuc 	}
703433d6423SLionel Sambuc 
704433d6423SLionel Sambuc 	/* fill out sector with zero bytes */
705433d6423SLionel Sambuc 
706433d6423SLionel Sambuc 	if((rest=(ISO_SECTOR-(written % ISO_SECTOR)))) {
707433d6423SLionel Sambuc 		memset(buf, 0, rest);
708433d6423SLionel Sambuc 		Write(fd, buf, rest);
709433d6423SLionel Sambuc 		written += rest;
710433d6423SLionel Sambuc 	}
711433d6423SLionel Sambuc 
712433d6423SLionel Sambuc 	/* update dir size with padded size */
713433d6423SLionel Sambuc 
714433d6423SLionel Sambuc 	if(root->isdir) { root->bytesize = written; }
715433d6423SLionel Sambuc 
716433d6423SLionel Sambuc 	*currentsector += written/ISO_SECTOR;
717433d6423SLionel Sambuc }
718433d6423SLionel Sambuc 
719433d6423SLionel Sambuc void
writebootcatalog(int fd,int * currentsector,int imagesector,int imagesectors)720433d6423SLionel Sambuc writebootcatalog(int fd, int  *currentsector, int imagesector, int imagesectors)
721433d6423SLionel Sambuc {
722433d6423SLionel Sambuc 	static char buf[ISO_SECTOR];
723433d6423SLionel Sambuc 	struct bc_validation validate;
724433d6423SLionel Sambuc 	struct bc_initial initial;
725433d6423SLionel Sambuc 
726433d6423SLionel Sambuc 	ssize_t written, rest;
727433d6423SLionel Sambuc 	u_int16_t *v, sum = 0;
728433d6423SLionel Sambuc 	int i;
729433d6423SLionel Sambuc 
730433d6423SLionel Sambuc 	/* write validation entry */
731433d6423SLionel Sambuc 
732433d6423SLionel Sambuc 	memset(&validate, 0, sizeof(validate));
733433d6423SLionel Sambuc 	validate.headerid = 1;
734433d6423SLionel Sambuc 	validate.platform = PLATFORM_80X86;
735433d6423SLionel Sambuc 	strcpy(validate.idstring, "");
736433d6423SLionel Sambuc 	validate.keys[0] = 0x55;
737433d6423SLionel Sambuc 	validate.keys[1] = 0xaa;
738433d6423SLionel Sambuc 
739433d6423SLionel Sambuc 	v = (u_int16_t *) &validate;
740433d6423SLionel Sambuc 	for(i = 0; i < sizeof(validate)/2; i++)
741433d6423SLionel Sambuc 		sum += v[i];
742433d6423SLionel Sambuc 	validate.checksum = 65535 - sum + 1; /* sum must be 0 */
743433d6423SLionel Sambuc 
744433d6423SLionel Sambuc 	written = Write(fd, &validate, sizeof(validate));
745433d6423SLionel Sambuc 
746433d6423SLionel Sambuc 	/* write initial/default entry */
747433d6423SLionel Sambuc 
748433d6423SLionel Sambuc 	memset(&initial, 0, sizeof(initial));
749433d6423SLionel Sambuc 
750433d6423SLionel Sambuc 	initial.indicator = INDICATE_BOOTABLE;
751433d6423SLionel Sambuc 	initial.media = bootmedia;
752433d6423SLionel Sambuc 	initial.seg = (u_int16_t) (bootseg & 0xFFFF);
753433d6423SLionel Sambuc 	initial.sectors = 1;
754433d6423SLionel Sambuc 	if (bootmedia == BOOTMEDIA_HARDDISK)
755433d6423SLionel Sambuc 	{
756433d6423SLionel Sambuc 		initial.type = system_type;
757433d6423SLionel Sambuc 	}
758433d6423SLionel Sambuc 	if (bootmedia == BOOTMEDIA_NONE)
759433d6423SLionel Sambuc 	{
760433d6423SLionel Sambuc 		initial.sectors = imagesectors;
761433d6423SLionel Sambuc 	}
762433d6423SLionel Sambuc 	initial.startsector = imagesector;
763433d6423SLionel Sambuc 
764433d6423SLionel Sambuc 	written += Write(fd, &initial, sizeof(initial));
765433d6423SLionel Sambuc 
766433d6423SLionel Sambuc 	/* fill out the rest of the sector with 0's */
767433d6423SLionel Sambuc 
768433d6423SLionel Sambuc 	if((rest = ISO_SECTOR - (written % ISO_SECTOR))) {
769433d6423SLionel Sambuc 		memset(buf, 0, sizeof(buf));
770433d6423SLionel Sambuc 		written += Write(fd, buf, rest);
771433d6423SLionel Sambuc 	}
772433d6423SLionel Sambuc 
773433d6423SLionel Sambuc 	(*currentsector) += written / ISO_SECTOR;
774433d6423SLionel Sambuc 
775433d6423SLionel Sambuc 	return;
776433d6423SLionel Sambuc }
777433d6423SLionel Sambuc 
778433d6423SLionel Sambuc int
writebootimage(char * bootimage,int bootfd,int fd,int * currentsector,char * appendsectorinfo,struct node * root)779433d6423SLionel Sambuc writebootimage(char *bootimage, int bootfd, int fd, int *currentsector,
780433d6423SLionel Sambuc 	char *appendsectorinfo, struct node *root)
781433d6423SLionel Sambuc {
782433d6423SLionel Sambuc 	static unsigned char buf[1024*64], *addr;
783433d6423SLionel Sambuc 	ssize_t written = 0, rest;
784433d6423SLionel Sambuc 	int virtuals, rem;
785433d6423SLionel Sambuc 	struct stat sb;
786433d6423SLionel Sambuc 	struct bap {
787433d6423SLionel Sambuc 		off_t sector;
788433d6423SLionel Sambuc 		int length;
789433d6423SLionel Sambuc 	} bap[2];
790433d6423SLionel Sambuc 
791433d6423SLionel Sambuc 	bap[0].length = bap[0].sector = 0;
792433d6423SLionel Sambuc 	bap[1].length = bap[1].sector = 0;
793433d6423SLionel Sambuc 
794433d6423SLionel Sambuc 	if (fstat(bootfd, &sb) < 0) {
795433d6423SLionel Sambuc 		perror("stat boot image");
796433d6423SLionel Sambuc 		exit(1);
797433d6423SLionel Sambuc 	}
798433d6423SLionel Sambuc 
799433d6423SLionel Sambuc 	rem = sb.st_size;
800433d6423SLionel Sambuc 
801433d6423SLionel Sambuc 	while(rem > 0) {
802433d6423SLionel Sambuc 		int want;
803433d6423SLionel Sambuc 		want = rem < sizeof(buf) ? rem : sizeof(buf);
804433d6423SLionel Sambuc 		Read(bootfd, buf, want);
805433d6423SLionel Sambuc 		if (written == 0) {
806433d6423SLionel Sambuc 			/* check some properties at beginning. */
807433d6423SLionel Sambuc 			if (buf[0] == 1 && buf[1] == 3) {
808433d6423SLionel Sambuc 				fprintf(stderr, "boot image %s is an a.out executable\n",
809433d6423SLionel Sambuc 						bootimage);
810433d6423SLionel Sambuc 				exit(1);
811433d6423SLionel Sambuc 			}
812433d6423SLionel Sambuc 			if (rem >= VIRTUAL_SECTOR
813433d6423SLionel Sambuc 			  && (buf[510] != 0x55 || buf[511] != 0xaa) ) {
814433d6423SLionel Sambuc 				fprintf(stderr, "invalid boot sector (bad magic.)\n");
815433d6423SLionel Sambuc 				exit(1);
816433d6423SLionel Sambuc 			}
817433d6423SLionel Sambuc 		}
818433d6423SLionel Sambuc 		written += Write(fd, buf, want);
819433d6423SLionel Sambuc 		rem -= want;
820433d6423SLionel Sambuc 	}
821433d6423SLionel Sambuc 
822433d6423SLionel Sambuc 	if(appendsectorinfo) {
823433d6423SLionel Sambuc 		struct node *n;
824433d6423SLionel Sambuc 		for(n = root->firstchild; n; n = n ->nextchild) {
825433d6423SLionel Sambuc 			if(!strcasecmp(appendsectorinfo, n->name)) {
826433d6423SLionel Sambuc 				bap[0].sector = n->startsector;
827433d6423SLionel Sambuc 				bap[0].length = ROUNDUP(n->bytesize, ISO_SECTOR);
828433d6423SLionel Sambuc 				break;
829433d6423SLionel Sambuc 			}
830433d6423SLionel Sambuc 		}
831433d6423SLionel Sambuc 		if(!n) {
832433d6423SLionel Sambuc 			fprintf(stderr, "%s not found in root.\n",
833433d6423SLionel Sambuc 				appendsectorinfo);
834433d6423SLionel Sambuc 			exit(1);
835433d6423SLionel Sambuc 		}
836433d6423SLionel Sambuc 
837*7c48de6cSDavid van Moolenbroek 		fprintf(stderr,
838*7c48de6cSDavid van Moolenbroek 			" * appended sector info: 0x%"PRIx64" len 0x%x\n",
839433d6423SLionel Sambuc 			bap[0].sector, bap[0].length);
840433d6423SLionel Sambuc 
841433d6423SLionel Sambuc 		addr = buf;
842433d6423SLionel Sambuc 		addr[0] = bap[0].length;
843433d6423SLionel Sambuc 		assert(addr[0] > 0);
844433d6423SLionel Sambuc 		addr[1] = (bap[0].sector >>  0) & 0xFF;
845433d6423SLionel Sambuc 		addr[2] = (bap[0].sector >>  8) & 0xFF;
846433d6423SLionel Sambuc 		addr[3] = (bap[0].sector >> 16) & 0xFF;
847433d6423SLionel Sambuc 		addr[4] = 0;
848433d6423SLionel Sambuc 		addr[5] = 0;
849433d6423SLionel Sambuc 
850433d6423SLionel Sambuc 		written += Write(fd, addr, 6);
851433d6423SLionel Sambuc 	}
852433d6423SLionel Sambuc 
853433d6423SLionel Sambuc         virtuals = ROUNDUP(written, VIRTUAL_SECTOR);
854433d6423SLionel Sambuc         assert(virtuals * VIRTUAL_SECTOR >= written);
855433d6423SLionel Sambuc 
856433d6423SLionel Sambuc 	if((rest = ISO_SECTOR - (written % ISO_SECTOR))) {
857433d6423SLionel Sambuc 		memset(buf, 0, sizeof(buf));
858433d6423SLionel Sambuc 		written += Write(fd, buf, rest);
859433d6423SLionel Sambuc 	}
860433d6423SLionel Sambuc 
861433d6423SLionel Sambuc 	(*currentsector) += written/ISO_SECTOR;
862433d6423SLionel Sambuc 
863433d6423SLionel Sambuc 	return virtuals;
864433d6423SLionel Sambuc }
865433d6423SLionel Sambuc 
866433d6423SLionel Sambuc void
writebootrecord(int fd,int * currentsector,int bootcatalogsector)867433d6423SLionel Sambuc writebootrecord(int fd, int *currentsector, int bootcatalogsector)
868433d6423SLionel Sambuc {
869433d6423SLionel Sambuc 	int i;
870433d6423SLionel Sambuc 	static struct bootrecord bootrecord;
871433d6423SLionel Sambuc 	ssize_t w = 0;
872433d6423SLionel Sambuc 	/* boot record volume descriptor */
873433d6423SLionel Sambuc 
874433d6423SLionel Sambuc 	memset(&bootrecord, 0, sizeof(bootrecord));
875433d6423SLionel Sambuc 	bootrecord.set[0] = 'C';
876433d6423SLionel Sambuc 	bootrecord.set[1] = 'D';
877433d6423SLionel Sambuc 	bootrecord.set[2] = '0';
878433d6423SLionel Sambuc 	bootrecord.set[3] = '0';
879433d6423SLionel Sambuc 	bootrecord.set[4] = '1';
880433d6423SLionel Sambuc 	bootrecord.version = 1;
881433d6423SLionel Sambuc 	bootrecord.bootcatalog = bootcatalogsector;
882433d6423SLionel Sambuc 	strcpy(bootrecord.ident, "EL TORITO SPECIFICATION");
883433d6423SLionel Sambuc 	for(i = strlen(bootrecord.ident);
884433d6423SLionel Sambuc 		i < sizeof(bootrecord.ident); i++)
885433d6423SLionel Sambuc 		bootrecord.ident[i] = '\0';
886433d6423SLionel Sambuc 
887433d6423SLionel Sambuc 	w  = Writefield(fd, bootrecord.indicator);
888433d6423SLionel Sambuc 	w += Writefield(fd, bootrecord.set);
889433d6423SLionel Sambuc 	w += Writefield(fd, bootrecord.version);
890433d6423SLionel Sambuc 	w += Writefield(fd, bootrecord.ident);
891433d6423SLionel Sambuc 	w += Writefield(fd, bootrecord.zero);
892433d6423SLionel Sambuc 	w += Writefield(fd, bootrecord.bootcatalog);
893433d6423SLionel Sambuc 	w += Writefield(fd, bootrecord.zero2);
894433d6423SLionel Sambuc 
895433d6423SLionel Sambuc 	if(w != ISO_SECTOR) {
896*7c48de6cSDavid van Moolenbroek 		fprintf(stderr, "WARNING: something went wrong - "
897*7c48de6cSDavid van Moolenbroek 			"boot record (%zd) isn't a sector size (%d)\n",
898433d6423SLionel Sambuc 			w, ISO_SECTOR);
899433d6423SLionel Sambuc 	}
900433d6423SLionel Sambuc 
901433d6423SLionel Sambuc 	(*currentsector)++;
902433d6423SLionel Sambuc }
903433d6423SLionel Sambuc 
904433d6423SLionel Sambuc int
main(int argc,char * argv[])905433d6423SLionel Sambuc main(int argc, char *argv[])
906433d6423SLionel Sambuc {
907433d6423SLionel Sambuc 	int currentsector = 0;
908433d6423SLionel Sambuc 	int imagesector, imagesectors;
909433d6423SLionel Sambuc 	int bootfd, fd, i, ch, nsectors;
910433d6423SLionel Sambuc 	int remove_after = 0;
911433d6423SLionel Sambuc 	static char block[ISO_SECTOR];
912433d6423SLionel Sambuc 	static struct pvd pvd;
913433d6423SLionel Sambuc 	char *label = "ISO9660";
914433d6423SLionel Sambuc 	struct tm *now;
915433d6423SLionel Sambuc 	time_t nowtime;
916433d6423SLionel Sambuc 	char timestr[20], *prog;
917433d6423SLionel Sambuc 	char *bootimage = NULL;
918433d6423SLionel Sambuc 	struct node root;
919433d6423SLionel Sambuc 	int pvdsector;
920433d6423SLionel Sambuc 	int bigpath, littlepath, pathbytes = 0, dirsector, filesector, enddir;
921433d6423SLionel Sambuc 	int bootvolumesector, bootcatalogsector;
922433d6423SLionel Sambuc 	char *appendsectorinfo = NULL;
923433d6423SLionel Sambuc 
924433d6423SLionel Sambuc 	prog = argv[0];
925433d6423SLionel Sambuc 
926433d6423SLionel Sambuc 	/* This check is to prevent compiler padding screwing up
927433d6423SLionel Sambuc 	 * our format.
928433d6423SLionel Sambuc 	 */
929433d6423SLionel Sambuc 
930433d6423SLionel Sambuc 	if(sizeof(struct pvd) != ISO_SECTOR) {
931433d6423SLionel Sambuc 		fprintf(stderr, "Something confusing happened at\n"
932*7c48de6cSDavid van Moolenbroek 			"compile-time; pvd should be a sector size. "
933*7c48de6cSDavid van Moolenbroek 			"%zd != %d\n", sizeof(struct pvd), ISO_SECTOR);
934433d6423SLionel Sambuc 		return 1;
935433d6423SLionel Sambuc 	}
936433d6423SLionel Sambuc 
937433d6423SLionel Sambuc 	while ((ch = getopt(argc, argv, "a:b:B:s:Rb:hl:nfF")) != -1) {
938433d6423SLionel Sambuc 		switch(ch) {
939433d6423SLionel Sambuc 			case 's':
940433d6423SLionel Sambuc 				if(optarg[0] != '0' || optarg[1] != 'x') {
941433d6423SLionel Sambuc 					fprintf(stderr, "%s: -s<hex>\n",
942433d6423SLionel Sambuc 						argv[0]);
943433d6423SLionel Sambuc 					return 1;
944433d6423SLionel Sambuc 				}
945433d6423SLionel Sambuc 				bootseg = strtoul(optarg+2, NULL, 16);
946433d6423SLionel Sambuc 				break;
947433d6423SLionel Sambuc 			case 'h':
948433d6423SLionel Sambuc 				bootmedia= BOOTMEDIA_HARDDISK;
949433d6423SLionel Sambuc 				break;
950433d6423SLionel Sambuc 			case 'n':
951433d6423SLionel Sambuc 				bootmedia= BOOTMEDIA_NONE;
952433d6423SLionel Sambuc 				break;
953433d6423SLionel Sambuc 			case 'f':
954433d6423SLionel Sambuc 				bootmedia= BOOTMEDIA_1440K;
955433d6423SLionel Sambuc 				break;
956433d6423SLionel Sambuc 			case 'F':
957433d6423SLionel Sambuc 				bootmedia= BOOTMEDIA_2880K;
958433d6423SLionel Sambuc 				break;
959433d6423SLionel Sambuc 			case 'a':
960433d6423SLionel Sambuc 				if(!(appendsectorinfo = strdup(optarg)))
961433d6423SLionel Sambuc 					exit(1);
962433d6423SLionel Sambuc 				break;
963433d6423SLionel Sambuc 			case 'l':
964433d6423SLionel Sambuc 				label = optarg;
965433d6423SLionel Sambuc 				break;
966433d6423SLionel Sambuc 			case 'R':
967433d6423SLionel Sambuc 				remove_after = 1;
968433d6423SLionel Sambuc 				break;
969433d6423SLionel Sambuc 			case 'B':
970433d6423SLionel Sambuc 				bootimage = optarg;
971433d6423SLionel Sambuc 				if((bootfd = open(bootimage, O_RDONLY)) < 0) {
972433d6423SLionel Sambuc 					perror(bootimage);
973433d6423SLionel Sambuc 					return 1;
974433d6423SLionel Sambuc 				}
975433d6423SLionel Sambuc 				break;
976433d6423SLionel Sambuc 		}
977433d6423SLionel Sambuc 	}
978433d6423SLionel Sambuc 
979433d6423SLionel Sambuc 	argc -= optind;
980433d6423SLionel Sambuc 	argv += optind;
981433d6423SLionel Sambuc 
982433d6423SLionel Sambuc 	/* Args check */
983433d6423SLionel Sambuc 
984433d6423SLionel Sambuc 	if(argc != 2) {
985433d6423SLionel Sambuc 		fprintf(stderr, "usage: %s [-l <label>] [-(b|B) <bootimage> [-n|-f|-F|-h] [-s <bootsegment>] [ -a <appendfile> ] <dir> <isofile>\n",
986433d6423SLionel Sambuc 			prog);
987433d6423SLionel Sambuc 		return 1;
988433d6423SLionel Sambuc 	}
989433d6423SLionel Sambuc 
990433d6423SLionel Sambuc 	if((bootimage && bootmedia == BOOTMEDIA_UNSPECIFIED) ||
991433d6423SLionel Sambuc 		(!bootimage && bootmedia != BOOTMEDIA_UNSPECIFIED)) {
992433d6423SLionel Sambuc 		fprintf(stderr, "%s: provide both boot image and boot type or neither.\n",
993433d6423SLionel Sambuc 			prog);
994433d6423SLionel Sambuc 		return 1;
995433d6423SLionel Sambuc 	}
996433d6423SLionel Sambuc 
997433d6423SLionel Sambuc 	if(!bootimage && bootseg) {
998433d6423SLionel Sambuc 		fprintf(stderr, "%s: boot seg provided but no boot image\n",
999433d6423SLionel Sambuc 			prog);
1000433d6423SLionel Sambuc 		return 1;
1001433d6423SLionel Sambuc 	}
1002433d6423SLionel Sambuc 
1003433d6423SLionel Sambuc 	if(appendsectorinfo) {
1004433d6423SLionel Sambuc 		if(!bootimage || bootmedia != BOOTMEDIA_NONE) {
1005433d6423SLionel Sambuc 			fprintf(stderr, "%s: append sector info where?\n",
1006433d6423SLionel Sambuc 				prog);
1007433d6423SLionel Sambuc 			return 1;
1008433d6423SLionel Sambuc 		}
1009433d6423SLionel Sambuc 	}
1010433d6423SLionel Sambuc 
1011433d6423SLionel Sambuc 	/* create .iso file */
1012433d6423SLionel Sambuc 
1013433d6423SLionel Sambuc 	if((fd=open(argv[1], O_WRONLY | O_TRUNC | O_CREAT, 0600)) < 0) {
1014433d6423SLionel Sambuc 		perror(argv[1]);
1015433d6423SLionel Sambuc 		return 1;
1016433d6423SLionel Sambuc 	}
1017433d6423SLionel Sambuc 
1018433d6423SLionel Sambuc 	/* go to where the iso has to be made from */
1019433d6423SLionel Sambuc 
1020433d6423SLionel Sambuc 	if(chdir(argv[0]) < 0) {
1021433d6423SLionel Sambuc 		perror(argv[0]);
1022433d6423SLionel Sambuc 		return 1;
1023433d6423SLionel Sambuc 	}
1024433d6423SLionel Sambuc 
1025433d6423SLionel Sambuc 	/* collect dirs and files */
1026433d6423SLionel Sambuc 
1027433d6423SLionel Sambuc 	fprintf(stderr, " * traversing input tree\n");
1028433d6423SLionel Sambuc 
1029433d6423SLionel Sambuc 	maketree(&root, "", 1);
1030433d6423SLionel Sambuc 
1031433d6423SLionel Sambuc 	fprintf(stderr, " * writing initial zeroes and pvd\n");
1032433d6423SLionel Sambuc 
1033433d6423SLionel Sambuc 	/* first sixteen sectors are zero */
1034433d6423SLionel Sambuc 
1035433d6423SLionel Sambuc 	memset(block, 0, sizeof(block));
1036433d6423SLionel Sambuc 
1037433d6423SLionel Sambuc 	for(i = 0; i < 16; i++)
1038433d6423SLionel Sambuc 		writesector(fd, block, &currentsector);
1039433d6423SLionel Sambuc 
1040433d6423SLionel Sambuc 	/* Primary Volume Descriptor */
1041433d6423SLionel Sambuc 	memset(&pvd, 0, sizeof(pvd));
1042433d6423SLionel Sambuc 	pvd.one = 1;
1043433d6423SLionel Sambuc 	pvd.set[0] = 67;
1044433d6423SLionel Sambuc 	pvd.set[1] = 68;
1045433d6423SLionel Sambuc 	pvd.set[2] = 48;
1046433d6423SLionel Sambuc 	pvd.set[3] = 48;
1047433d6423SLionel Sambuc 	pvd.set[4] = 49;
1048433d6423SLionel Sambuc 	pvd.set[5] =  1;
1049433d6423SLionel Sambuc 	pvd.set[5] =  1;
1050433d6423SLionel Sambuc 
1051433d6423SLionel Sambuc 	strncpy(pvd.volume, label, sizeof(pvd.volume)-1);
1052433d6423SLionel Sambuc 	for(i = strlen(pvd.volume); i < sizeof(pvd.volume); i++)
1053433d6423SLionel Sambuc 		pvd.volume[i] = ' ';
1054433d6423SLionel Sambuc 	for(i = 0; i < sizeof(pvd.system); i++)
1055433d6423SLionel Sambuc 		pvd.system[i] = ' ';
1056433d6423SLionel Sambuc 
1057433d6423SLionel Sambuc 	both16((unsigned char *) pvd.setsize, 1);
1058433d6423SLionel Sambuc 	both16((unsigned char *) pvd.seq, 1);
1059433d6423SLionel Sambuc 	both16((unsigned char *) pvd.sectorsize, ISO_SECTOR);
1060433d6423SLionel Sambuc 
1061433d6423SLionel Sambuc 	/* fill time fields */
1062433d6423SLionel Sambuc 	time(&nowtime);
1063433d6423SLionel Sambuc 	now = gmtime(&nowtime);
1064433d6423SLionel Sambuc 	strftime(timestr, sizeof(timestr), "%Y%m%d%H%M%S000", now);
1065433d6423SLionel Sambuc 	memcpy(pvd.create, timestr, strlen(timestr));
1066433d6423SLionel Sambuc 	memcpy(pvd.modified, timestr, strlen(timestr));
1067433d6423SLionel Sambuc 	memcpy(pvd.effective, timestr, strlen(timestr));
1068433d6423SLionel Sambuc 	strcpy(pvd.expiry, "0000000000000000");	/* not specified */
10698ca6466aSDavid van Moolenbroek 	pvd.one2 = 1;
1070433d6423SLionel Sambuc 	pvdsector = currentsector;
1071433d6423SLionel Sambuc 
1072433d6423SLionel Sambuc 	writesector(fd, (char *) &pvd, &currentsector);
1073433d6423SLionel Sambuc 
1074433d6423SLionel Sambuc 	if(bootimage) {
1075433d6423SLionel Sambuc 		fprintf(stderr, " * writing boot record volume descriptor\n");
1076433d6423SLionel Sambuc 		bootvolumesector = currentsector;
1077433d6423SLionel Sambuc 		writebootrecord(fd, &currentsector, 0);
1078433d6423SLionel Sambuc 	}
1079433d6423SLionel Sambuc 
1080433d6423SLionel Sambuc 	/* volume descriptor set terminator */
1081433d6423SLionel Sambuc 	memset(block, 0, sizeof(block));
1082433d6423SLionel Sambuc 	block[0] = 255;
1083433d6423SLionel Sambuc 	block[1] =  67;
1084433d6423SLionel Sambuc 	block[2] =  68;
1085433d6423SLionel Sambuc 	block[3] =  48;
1086433d6423SLionel Sambuc 	block[4] =  48;
1087433d6423SLionel Sambuc 	block[5] =  49;
1088433d6423SLionel Sambuc 	block[6] =   1;
1089433d6423SLionel Sambuc 
1090433d6423SLionel Sambuc 	writesector(fd, block, &currentsector);
1091433d6423SLionel Sambuc 
1092433d6423SLionel Sambuc 	if(bootimage) {
1093433d6423SLionel Sambuc 		/* write the boot catalog */
1094433d6423SLionel Sambuc 		fprintf(stderr, " * writing the boot catalog\n");
1095433d6423SLionel Sambuc 		bootcatalogsector = currentsector;
1096433d6423SLionel Sambuc 		if (bootmedia == BOOTMEDIA_HARDDISK)
1097433d6423SLionel Sambuc 			system_type = get_system_type(bootfd);
1098433d6423SLionel Sambuc 		writebootcatalog(fd, &currentsector, 0, 0);
1099433d6423SLionel Sambuc 
1100433d6423SLionel Sambuc 		/* write boot image */
1101433d6423SLionel Sambuc 		fprintf(stderr, " * writing the boot image\n");
1102433d6423SLionel Sambuc 		imagesector = currentsector;
1103433d6423SLionel Sambuc 		imagesectors = writebootimage(bootimage, bootfd,
1104433d6423SLionel Sambuc 			fd, &currentsector, NULL, &root);
1105433d6423SLionel Sambuc 		fprintf(stderr, " * image: %d %d-byte sectors @ cd sector 0x%x\n",
1106433d6423SLionel Sambuc 			imagesectors, VIRTUAL_SECTOR, imagesector);
1107433d6423SLionel Sambuc 	}
1108433d6423SLionel Sambuc 
1109433d6423SLionel Sambuc 	/* write out all the file data */
1110433d6423SLionel Sambuc 
1111433d6423SLionel Sambuc 	filesector = currentsector;
1112433d6423SLionel Sambuc 	fprintf(stderr, " * writing file data\n");
1113433d6423SLionel Sambuc 	writedata(NULL, &root, fd, &currentsector, 0,
1114433d6423SLionel Sambuc 		(struct dir *) &pvd.rootrecord, sizeof(pvd.rootrecord),
1115433d6423SLionel Sambuc 		remove_after);
1116433d6423SLionel Sambuc 
1117433d6423SLionel Sambuc 	/* write out all the dir data */
1118433d6423SLionel Sambuc 
1119433d6423SLionel Sambuc 	dirsector = currentsector;
1120433d6423SLionel Sambuc 	fprintf(stderr, " * writing dir data\n");
1121433d6423SLionel Sambuc 	writedata(NULL, &root, fd, &currentsector, 1,
1122433d6423SLionel Sambuc 		(struct dir *) &pvd.rootrecord, sizeof(pvd.rootrecord),
1123433d6423SLionel Sambuc 			remove_after);
1124433d6423SLionel Sambuc 	enddir = currentsector;
1125433d6423SLionel Sambuc 	seeksector(fd, dirsector, &currentsector);
1126433d6423SLionel Sambuc 	fprintf(stderr, " * rewriting dir data\n");
1127433d6423SLionel Sambuc 	fflush(NULL);
1128433d6423SLionel Sambuc 	writedata(NULL, &root, fd, &currentsector, 1,
1129433d6423SLionel Sambuc 		(struct dir *) &pvd.rootrecord, sizeof(pvd.rootrecord),
1130433d6423SLionel Sambuc 			remove_after);
1131433d6423SLionel Sambuc 	if(currentsector != enddir) {
1132433d6423SLionel Sambuc 		fprintf(stderr, "warning: inconsistent directories - "
1133433d6423SLionel Sambuc 			"I have a bug! iso may be broken.\n");
1134433d6423SLionel Sambuc 	}
1135433d6423SLionel Sambuc 
1136433d6423SLionel Sambuc 	/* now write the path table in both formats */
1137433d6423SLionel Sambuc 
1138433d6423SLionel Sambuc 	fprintf(stderr, " * writing big-endian path table\n");
1139433d6423SLionel Sambuc 	bigpath = currentsector;
1140433d6423SLionel Sambuc 	currentsector += makepathtables(&root, 0, &pathbytes, fd);
1141433d6423SLionel Sambuc 
1142433d6423SLionel Sambuc 	fprintf(stderr, " * writing little-endian path table\n");
1143433d6423SLionel Sambuc 	littlepath = currentsector;
1144433d6423SLionel Sambuc 	currentsector += makepathtables(&root, 1, &pathbytes, fd);
1145433d6423SLionel Sambuc 
1146433d6423SLionel Sambuc 	both32((unsigned char *) pvd.pathtablesize, pathbytes);
1147433d6423SLionel Sambuc 	little32((unsigned char *) &pvd.first_little_pathtable_start, littlepath);
1148433d6423SLionel Sambuc 	big32((unsigned char *) &pvd.first_big_pathtable_start, bigpath);
1149433d6423SLionel Sambuc 
1150433d6423SLionel Sambuc 	/* this is the size of the iso filesystem for use in the pvd later */
1151433d6423SLionel Sambuc 
1152433d6423SLionel Sambuc 	nsectors = currentsector;
1153433d6423SLionel Sambuc 	both32((unsigned char *) pvd.sectors, nsectors);
1154433d6423SLionel Sambuc 
1155433d6423SLionel Sambuc 	/* *********** Filesystem writing done ************************* */
1156433d6423SLionel Sambuc 
1157433d6423SLionel Sambuc 	/* finish and rewrite the pvd. */
1158433d6423SLionel Sambuc 	fprintf(stderr, " * rewriting pvd\n");
1159433d6423SLionel Sambuc 	seekwritesector(fd, pvdsector, (char *) &pvd, &currentsector);
1160433d6423SLionel Sambuc 
1161433d6423SLionel Sambuc 	/* write root dir entry in pvd */
1162433d6423SLionel Sambuc 	seeksector(fd, pvdsector, &currentsector);
1163433d6423SLionel Sambuc 	Lseek(fd, (int)((char *) &pvd.rootrecord - (char *) &pvd), SEEK_CUR);
1164433d6423SLionel Sambuc 	if(write_direntry(&root, CURRENTDIR, fd) > sizeof(pvd.rootrecord)) {
1165433d6423SLionel Sambuc 		fprintf(stderr, "warning: unexpectedly large root record\n");
1166433d6423SLionel Sambuc 	}
1167433d6423SLionel Sambuc 
1168433d6423SLionel Sambuc 	if(bootimage) {
1169433d6423SLionel Sambuc 		fprintf(stderr, " * rewriting boot catalog\n");
1170433d6423SLionel Sambuc 		seeksector(fd, bootcatalogsector, &currentsector);
1171433d6423SLionel Sambuc 		writebootcatalog(fd, &currentsector, imagesector, imagesectors);
1172433d6423SLionel Sambuc 
1173433d6423SLionel Sambuc 		/* finish and rewrite the boot record volume descriptor */
1174433d6423SLionel Sambuc 		fprintf(stderr, " * rewriting the boot rvd\n");
1175433d6423SLionel Sambuc 		seeksector(fd, bootvolumesector, &currentsector);
1176433d6423SLionel Sambuc 		writebootrecord(fd, &currentsector, bootcatalogsector);
1177433d6423SLionel Sambuc 
1178433d6423SLionel Sambuc 		if(appendsectorinfo) {
1179433d6423SLionel Sambuc 			Lseek(bootfd, 0, SEEK_SET);
1180433d6423SLionel Sambuc 			fprintf(stderr, " * rewriting boot image\n");
1181433d6423SLionel Sambuc 			seeksector(fd, imagesector, &currentsector);
1182433d6423SLionel Sambuc 			writebootimage(bootimage, bootfd,
1183433d6423SLionel Sambuc 				fd, &currentsector, appendsectorinfo, &root);
1184433d6423SLionel Sambuc 
1185433d6423SLionel Sambuc 		}
1186433d6423SLionel Sambuc 		close(bootfd);
1187433d6423SLionel Sambuc 	}
1188433d6423SLionel Sambuc 
1189433d6423SLionel Sambuc 	fprintf(stderr, " * all ok\n");
1190433d6423SLionel Sambuc 
1191433d6423SLionel Sambuc 	return 0;
1192433d6423SLionel Sambuc }
1193433d6423SLionel Sambuc 
get_system_type(int fd)1194433d6423SLionel Sambuc int get_system_type(int fd)
1195433d6423SLionel Sambuc {
1196433d6423SLionel Sambuc 	off_t old_pos;
1197433d6423SLionel Sambuc 	size_t size;
1198433d6423SLionel Sambuc 	ssize_t r;
1199433d6423SLionel Sambuc 	int type;
1200433d6423SLionel Sambuc 	struct part_entry *partp;
1201433d6423SLionel Sambuc 	unsigned char bootsector[512];
1202433d6423SLionel Sambuc 
1203433d6423SLionel Sambuc 	errno= 0;
1204433d6423SLionel Sambuc 	old_pos= lseek(fd, 0, SEEK_SET);
1205433d6423SLionel Sambuc 	if (old_pos == -1 && errno != 0)
1206433d6423SLionel Sambuc 	{
1207433d6423SLionel Sambuc 		fprintf(stderr, "bootimage file is not seekable: %s\n",
1208433d6423SLionel Sambuc 			strerror(errno));
1209433d6423SLionel Sambuc 		exit(1);
1210433d6423SLionel Sambuc 	}
1211433d6423SLionel Sambuc 	size= sizeof(bootsector);
1212433d6423SLionel Sambuc 	r= read(fd, bootsector, size);
1213433d6423SLionel Sambuc 	if (r != size)
1214433d6423SLionel Sambuc 	{
1215433d6423SLionel Sambuc 		fprintf(stderr, "error reading bootimage file: %s\n",
1216433d6423SLionel Sambuc 			r < 0 ? strerror(errno) : "unexpected EOF");
1217433d6423SLionel Sambuc 		exit(1);
1218433d6423SLionel Sambuc 	}
1219433d6423SLionel Sambuc 	if (bootsector[size-2] != 0x55 && bootsector[size-1] != 0xAA)
1220433d6423SLionel Sambuc 	{
1221433d6423SLionel Sambuc 		fprintf(stderr, "bad magic in bootimage file\n");
1222433d6423SLionel Sambuc 		exit(1);
1223433d6423SLionel Sambuc 	}
1224433d6423SLionel Sambuc 
1225433d6423SLionel Sambuc 	partp= (struct part_entry *)&bootsector[PART_TABLE_OFF];
1226433d6423SLionel Sambuc 	type= partp->sysind;
1227433d6423SLionel Sambuc 	if (type == NO_PART)
1228433d6423SLionel Sambuc 	{
1229433d6423SLionel Sambuc 		fprintf(stderr, "first partition table entry is unused\n");
1230433d6423SLionel Sambuc 		exit(1);
1231433d6423SLionel Sambuc 	}
1232433d6423SLionel Sambuc 	if (!(partp->bootind & ACTIVE_FLAG))
1233433d6423SLionel Sambuc 	{
1234433d6423SLionel Sambuc 		fprintf(stderr, "first partition table entry is not active\n");
1235433d6423SLionel Sambuc 		exit(1);
1236433d6423SLionel Sambuc 	}
1237433d6423SLionel Sambuc 
1238433d6423SLionel Sambuc 	lseek(fd, old_pos, SEEK_SET);
1239433d6423SLionel Sambuc 	return type;
1240433d6423SLionel Sambuc }
1241