|
root / base / usr / src / cmd / etdump
etdump Plain Text 788 lines 23.7 KB
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
#
# This file and its contents are supplied under the terms of the
# Common Development and Distribution License ("CDDL"), version 1.0.
# You may only use this file in accordance with the terms of version
# 1.0 of the CDDL.
#
# A full copy of the text of the CDDL should have accompanied this
# source.  A copy of the CDDL is also available via the Internet at
# http://www.illumos.org/license/CDDL.
#

#
# Copyright 2019 Toomas Soome <tsoome@me.com>
# Copyright (c) 2019, Joyent, Inc.
#

PROG=	etdump
OBJS=	etdump.o output_shell.o output_text.o
SRCS=	$(OBJS:%.o=%.c)

include ../Makefile.cmd

# comes from FreeBSD upstream
SMOFF += all_func_returns

.KEEP_STATE:

all: $(PROG)

install: all $(ROOTPROG)

clean:
	$(RM) $(OBJS)

$(PROG):	$(OBJS)
	$(LINK.c) $(OBJS) -o $@ $(LDLIBS)
	$(POST_PROCESS)

lint:

include ../Makefile.targ
cd9660_eltorito.h:
cd9660_conversion.c:
/*
 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
 *
 * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
 * Perez-Rathke and Ram Vedam.  All rights reserved.
 *
 * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
 * Alan Perez-Rathke and Ram Vedam.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
 * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
 * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 */

etdump.h:
etdump.c:
etdump.1:
output_shell.c:
output_text.c:
/*
 * Copyright (c) 2018 iXsystems, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
etdump utility command
/*
 * $NetBSD: cd9660_eltorito.h,v 1.6 2017/01/24 11:22:43 nonaka Exp $
 */

/*
 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
 *
 * Copyright (c) 2005 Daniel Watt, Walter Deignan, Ryan Gabrys, Alan
 * Perez-Rathke and Ram Vedam.  All rights reserved.
 *
 * This code was written by Daniel Watt, Walter Deignan, Ryan Gabrys,
 * Alan Perez-Rathke and Ram Vedam.
 *
 * Redistribution and use in source and binary forms, with or
 * without modification, are permitted provided that the following
 * conditions are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY DANIEL WATT, WALTER DEIGNAN, RYAN
 * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL DANIEL WATT, WALTER DEIGNAN, RYAN
 * GABRYS, ALAN PEREZ-RATHKE AND RAM VEDAM BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 */

#ifndef _CD9660_ELTORITO_H_
#define	_CD9660_ELTORITO_H_

#include <sys/types.h>
#include <sys/queue.h>

/* Boot defines */
#define	ET_ID		"EL TORITO SPECIFICATION"
#define	ET_SYS_X86	0
#define	ET_SYS_PPC	1
#define	ET_SYS_MAC	2
#define	ET_SYS_EFI	0xef	/* Platform ID at section header entry */

#define	ET_BOOT_ENTRY_SIZE 0x20

#define	ET_BOOTABLE		0x88
#define	ET_NOT_BOOTABLE		0

#define	ET_MEDIA_NOEM			0
#define	ET_MEDIA_12FDD			1
#define	ET_MEDIA_144FDD			2
#define	ET_MEDIA_288FDD			3
#define	ET_MEDIA_HDD			4

#define	ET_INDICATOR_HEADERMORE	0x90
#define	ET_INDICATOR_HEADERLAST	0x91
#define	ET_INDICATOR_EXTENSION	0x44

/* Boot Structures */

#define	ISODCL(from, to) (to - from + 1)

typedef struct _boot_volume_descriptor {
	uchar_t boot_record_indicator	[ISODCL(0x00, 0x00)];
	uchar_t identifier		[ISODCL(0x01, 0x05)];
	uchar_t version			[ISODCL(0x06, 0x06)];
	uchar_t boot_system_identifier	[ISODCL(0x07, 0x26)];
	uchar_t unused1			[ISODCL(0x27, 0x46)];
	uchar_t boot_catalog_pointer	[ISODCL(0x47, 0x4A)];
	uchar_t unused2			[ISODCL(0x4B, 0x7FF)];
} boot_volume_descriptor;

typedef struct _boot_catalog_validation_entry {
	uchar_t header_id		[ISODCL(0x00, 0x00)];
	uchar_t platform_id		[ISODCL(0x01, 0x01)];
	uchar_t reserved1		[ISODCL(0x02, 0x03)];
	uchar_t manufacturer		[ISODCL(0x04, 0x1B)];
	uchar_t checksum		[ISODCL(0x1C, 0x1D)];
	uchar_t key			[ISODCL(0x1E, 0x1F)];
} boot_catalog_validation_entry;

typedef struct _boot_catalog_initial_entry {
	uchar_t boot_indicator		[ISODCL(0x00, 0x00)];
	uchar_t media_type		[ISODCL(0x01, 0x01)];
	uchar_t load_segment		[ISODCL(0x02, 0x03)];
	uchar_t system_type		[ISODCL(0x04, 0x04)];
	uchar_t unused_1		[ISODCL(0x05, 0x05)];
	uchar_t sector_count		[ISODCL(0x06, 0x07)];
	uchar_t load_rba		[ISODCL(0x08, 0x0B)];
	uchar_t unused_2		[ISODCL(0x0C, 0x1F)];
} boot_catalog_initial_entry;

#define	ET_SECTION_HEADER_MORE		0x90
#define	ET_SECTION_HEADER_LAST		0x91

typedef struct _boot_catalog_section_header {
	uchar_t header_indicator	[ISODCL(0x00, 0x00)];
	uchar_t platform_id		[ISODCL(0x01, 0x01)];
	uchar_t num_section_entries	[ISODCL(0x02, 0x03)];
	uchar_t id_string		[ISODCL(0x04, 0x1F)];
} boot_catalog_section_header;

typedef struct _boot_catalog_section_entry {
	uchar_t boot_indicator		[ISODCL(0x00, 0x00)];
	uchar_t media_type		[ISODCL(0x01, 0x01)];
	uchar_t load_segment		[ISODCL(0x02, 0x03)];
	uchar_t system_type		[ISODCL(0x04, 0x04)];
	uchar_t unused_1		[ISODCL(0x05, 0x05)];
	uchar_t sector_count		[ISODCL(0x06, 0x07)];
	uchar_t load_rba		[ISODCL(0x08, 0x0B)];
	uchar_t selection_criteria	[ISODCL(0x0C, 0x0C)];
	uchar_t vendor_criteria		[ISODCL(0x0D, 0x1F)];
} boot_catalog_section_entry;

typedef struct _boot_catalog_section_entry_extension {
	uchar_t extension_indicator	[ISODCL(0x00, 0x00)];
	uchar_t flags			[ISODCL(0x01, 0x01)];
	uchar_t vendor_criteria		[ISODCL(0x02, 0x1F)];
} boot_catalog_section_entry_extension;

#define	ET_ENTRY_VE 1
#define	ET_ENTRY_IE 2
#define	ET_ENTRY_SH 3
#define	ET_ENTRY_SE 4
#define	ET_ENTRY_EX 5

struct boot_catalog_entry {
	char entry_type;
	union {
		boot_catalog_validation_entry		VE;
		boot_catalog_initial_entry		IE;
		boot_catalog_section_header		SH;
		boot_catalog_section_entry		SE;
		boot_catalog_section_entry_extension	EX;
	} entry_data;

	LIST_ENTRY(boot_catalog_entry) ll_struct;
};

/* Temporary structure */
struct cd9660_boot_image {
	char *filename;
	int size;
	int sector;			/* copied to LoadRBA */
	int num_sectors;
	unsigned int loadSegment;
	uchar_t targetMode;
	uchar_t system;
	uchar_t bootable;
	uchar_t platform_id;		/* for section header entry */
	/*
	 * If the boot image exists in the filesystem
	 * already, this is a pointer to that node. For the sake
	 * of simplicity in future versions, this pointer is only
	 * to the node in the primary volume. This SHOULD be done
	 * via a hashtable lookup.
	 */
	struct _cd9660node *boot_image_node;
	TAILQ_ENTRY(cd9660_boot_image) image_list;
	int serialno;
};

void cd9660_721(uint16_t, unsigned char *);

static __inline uint16_t
isonum_721(const unsigned char *p)
{

	return (p[0] | p[1] << 8);
}

static __inline uint32_t
isonum_731(const unsigned char *p)
{

	return (p[0] | p[1] << 8 | p[2] << 16 | p[3] << 24);
}

#endif /* _CD9660_ELTORITO_H_ */
/*
 * Copyright (c) 2018 iXsystems, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <err.h>
#include <getopt.h>
#include <libgen.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/queue.h>
#include <endian.h>

#include <sys/fs/hsfs_isospec.h>
#include "cd9660_eltorito.h"

#include "etdump.h"

#define	ISO_DEFAULT_BLOCK_SHIFT	11
#define	ISO_DEFAULT_BLOCK_SIZE	(1 << ISO_DEFAULT_BLOCK_SHIFT)

/* Little endian */
void
cd9660_721(uint16_t w, unsigned char *twochar)
{
#if BYTE_ORDER == BIG_ENDIAN
	w = htole16(w);
#endif
	memcpy(twochar, &w, 2);
}

const char *
system_id_string(uchar_t system_id)
{

	switch (system_id) {
	case ET_SYS_X86:
		return ("i386");
	case ET_SYS_PPC:
		return ("powerpc");
	case ET_SYS_MAC:
		return ("mac");
	case ET_SYS_EFI:
		return ("efi");
	default:
		return ("invalid");
	}
}

const char *
media_type_string(uchar_t media_type)
{

	switch (media_type) {
	case ET_MEDIA_NOEM:
		return ("no emulation");
	case ET_MEDIA_12FDD:
		return ("1.2MB FDD");
	case ET_MEDIA_144FDD:
		return ("1.44MB FDD");
	case ET_MEDIA_288FDD:
		return ("2.88MB FDD");
	case ET_MEDIA_HDD:
		return ("HDD");
	default:
		return ("invalid");
	}
}

static int
read_sector(FILE *iso, daddr_t sector, char *buffer)
{

	fseek(iso, sector * ISO_DEFAULT_BLOCK_SIZE, SEEK_SET);
	if (fread(buffer, ISO_DEFAULT_BLOCK_SIZE, 1, iso) != 1) {
		return (errno);
	}
	return (0);
}

static bool
boot_catalog_valid(char *entry)
{
	boot_catalog_validation_entry *ve;
	int16_t		checksum, sum;
	unsigned char	*csptr;
	size_t		i;

	ve = (boot_catalog_validation_entry *)entry;

	checksum = isonum_721(ve->checksum);
	cd9660_721(0, ve->checksum);
	csptr = (unsigned char *)ve;

	for (i = sum = 0; i < sizeof (*ve); i += 2) {
		sum += (int16_t)csptr[i];
		sum += 256 * (int16_t)csptr[i + 1];
	}
	if (sum + checksum != 0) {
		return (false);
	}

	cd9660_721(checksum, ve->checksum);
	return (true);
}

static int
dump_section(char *buffer, size_t offset, FILE *outfile, const char *filename,
    struct outputter *outputter)
{
	boot_catalog_section_header *sh;
	uchar_t platform_id;
	int i;
	size_t entry_offset;
	boot_catalog_section_entry *entry;

	sh = (boot_catalog_section_header *)&buffer[offset];
	if (outputter->output_section != NULL) {
		outputter->output_section(outfile, filename, sh);
	}

	platform_id = sh->platform_id[0];

	if (outputter->output_entry != NULL) {
		for (i = 1; i <= (int)sh->num_section_entries[0]; i++) {
			entry_offset = offset + i * ET_BOOT_ENTRY_SIZE;
			entry =
			    (boot_catalog_section_entry *)&buffer[entry_offset];
			outputter->output_entry(outfile, filename, entry,
			    platform_id, false);
		}
	}

	return (1 + (int)sh->num_section_entries[0]);
}

static void
dump_eltorito(FILE *iso, const char *filename, FILE *outfile,
    struct outputter *outputter)
{
	char buffer[ISO_DEFAULT_BLOCK_SIZE], *entry;
	boot_volume_descriptor *bvd;
	daddr_t boot_catalog;
	size_t offset;
	int entry_count;

	if (read_sector(iso, 17, buffer) != 0)
		err(1, "failed to read from image");

	bvd = (boot_volume_descriptor *)buffer;
	if (memcmp(bvd->identifier, ISO_ID_STRING, 5) != 0)
		warnx("%s: not a valid ISO", filename);
	if (bvd->boot_record_indicator[0] != ISO_VD_BOOT)
		warnx("%s: not an El Torito bootable ISO", filename);
	if (memcmp(bvd->boot_system_identifier, ET_ID, 23) != 0)
		warnx("%s: not an El Torito bootable ISO", filename);

	boot_catalog = isonum_731(bvd->boot_catalog_pointer);

	if (read_sector(iso, boot_catalog, buffer) != 0)
		err(1, "failed to read from image");

	entry = buffer;
	offset = 0;

	if (!boot_catalog_valid(entry))
		warnx("%s: boot catalog checksum is invalid", filename);

	if (outputter->output_image != NULL)
		outputter->output_image(outfile, filename, bvd);

	offset += ET_BOOT_ENTRY_SIZE;
	entry = &buffer[offset];
	if (outputter->output_entry != NULL)
		outputter->output_entry(outfile, filename,
		    (boot_catalog_section_entry *)entry, 0, true);

	offset += ET_BOOT_ENTRY_SIZE;

	while (offset < ISO_DEFAULT_BLOCK_SIZE) {
		entry = &buffer[offset];

		if ((uint8_t)entry[0] != ET_SECTION_HEADER_MORE &&
		    (uint8_t)entry[0] != ET_SECTION_HEADER_LAST)
			break;

		entry_count = dump_section(buffer, offset, outfile, filename,
		    outputter);

		offset += entry_count * ET_BOOT_ENTRY_SIZE;
	}
}

static void
usage(const char *progname)
{
	char *path;

	path = strdup(progname);

	fprintf(stderr, "usage: %s [-f format] [-o filename] filename [...]\n",
	    basename(path));
	fprintf(stderr, "\tsupported output formats: shell, text\n");
	exit(1);
}

int
main(int argc, char **argv)
{
	int ch, i;
	FILE *outfile, *iso;
	struct outputter *outputter;

	outfile = stdout;
	outputter = output_text;

	static struct option longopts[] = {
		{ "format",	required_argument,	NULL,	'f' },
		{ "output",	required_argument,	NULL,	'o' },
		{ NULL,		0,			NULL,	0 },
	};

	while ((ch = getopt_long(argc, argv, "f:o:", longopts, NULL)) != -1) {
		switch (ch) {
		case 'f':
			if (strcmp(optarg, "shell") == 0)
				outputter = output_shell;
			else if (strcmp(optarg, "text") == 0)
				outputter = output_text;
			else
				usage(argv[0]);
			break;
		case 'o':
			if (strcmp(optarg, "-") == 0) {
				outfile = stdout;
			} else if ((outfile = fopen(optarg, "w")) == NULL) {
				err(1, "unable to open %s for output", optarg);
			}
			break;
		default:
			usage(argv[0]);
		}
	}

	argc -= optind;
	argv += optind;

	for (i = 0; i < argc; i++) {
		if (strcmp(argv[i], "-") == 0) {
			iso = stdin;
		} else {
			iso = fopen(argv[i], "r");
			if (iso == NULL)
				err(1, "could not open %s", argv[1]);
		}
		dump_eltorito(iso, argv[i], outfile, outputter);
	}
	return (0);
}
/*
 * Copyright (c) 2018 iXsystems, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $FreeBSD: head/usr.bin/etdump/etdump.h 331949 2018-04-03 21:08:10Z benno $
 */

#ifndef	_ETDUMP_H_
#define	_ETDUMP_H_

struct outputter {
	void (*output_image)(FILE *outfile, const char *filename,
	    boot_volume_descriptor *bvd);
	void (*output_section)(FILE *outfile, const char *filename,
	    boot_catalog_section_header *bcsh);
	void (*output_entry)(FILE *outfile, const char *filename,
	    boot_catalog_section_entry *bcse,
	    uchar_t platform_id, bool initial);
};

extern struct outputter *output_text;
extern struct outputter *output_shell;

const char *system_id_string(uchar_t system_id);
const char *media_type_string(uchar_t media_type);

#endif	/* _ETDUMP_H_ */
/*
 * Copyright (c) 2018 iXsystems, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <stdbool.h>
#include <stdio.h>
#include <inttypes.h>

#include "cd9660_eltorito.h"

#include "etdump.h"

static void
output_entry(FILE *outfile, const char *filename __unused,
    boot_catalog_section_entry *bcse, uchar_t platform_id, bool initial)
{
	const char *platform;

	switch (bcse->boot_indicator[0]) {
	case ET_BOOTABLE:
		break;
	case ET_NOT_BOOTABLE:
	default:
		return;
	}

	if (initial)
		platform = "default";
	else
		platform = system_id_string(platform_id);

	fprintf(outfile,
	    "et_platform=%s;et_system=%s;et_lba=%" PRIu32 ";et_sectors=%"
	    PRIu16 "\n", platform, system_id_string(bcse->system_type[0]),
	    isonum_731(bcse->load_rba), isonum_721(bcse->sector_count));
}

static struct outputter _output_shell = {
	.output_image = NULL,
	.output_section = NULL,
	.output_entry = output_entry,
};

struct outputter *output_shell = &_output_shell;
/*
 * Copyright (c) 2018 iXsystems, Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <stdbool.h>
#include <stdio.h>
#include <inttypes.h>

#include "cd9660_eltorito.h"
#include "etdump.h"

static void
output_image(FILE *outfile, const char *filename,
    boot_volume_descriptor *bvd __unused)
{

	fprintf(outfile, "Image in %s\n", filename);
}

static void
output_section(FILE *outfile, const char *filename __unused,
    boot_catalog_section_header *bcsh)
{

	fprintf(outfile, "\nSection header: %s",
	    system_id_string(bcsh->platform_id[0]));

	if (bcsh->header_indicator[0] == ET_SECTION_HEADER_LAST)
		fprintf(outfile, ", final\n");
	else
		fprintf(outfile, "\n");
}

static void
output_entry(FILE *outfile, const char *filename __unused,
    boot_catalog_section_entry *bcse, uchar_t platform_id __unused,
    bool initial)
{
	const char *indent;

	switch (bcse->boot_indicator[0]) {
	case ET_BOOTABLE:
		break;
	case ET_NOT_BOOTABLE:
	default:
		return;
	}

	if (initial) {
		fprintf(outfile, "Default entry\n");
		indent = "\t";
	} else {
		fprintf(outfile, "\tSection entry\n");
		indent = "\t\t";
	}

	fprintf(outfile, "%sSystem %s\n", indent,
	    system_id_string(bcse->system_type[0]));
	fprintf(outfile, "%sStart LBA %" PRIu32 " (0x%" PRIx32
	    "), sector count %" PRIu16 " (0x%" PRIx16 ")\n",
	    indent, isonum_731(bcse->load_rba), isonum_731(bcse->load_rba),
	    isonum_721(bcse->sector_count), isonum_721(bcse->sector_count));
	fprintf(outfile, "%sMedia type: %s\n", indent,
	    media_type_string(bcse->media_type[0]));
}

static struct outputter _output_text = {
	.output_image = output_image,
	.output_section = output_section,
	.output_entry = output_entry,
};

struct outputter *output_text = &_output_text;