xref: /illumos-gate/usr/src/cmd/etdump/etdump.c (revision 916bf6b9)
1c0455f33SToomas Soome /*
2c0455f33SToomas Soome  * Copyright (c) 2018 iXsystems, Inc.
3c0455f33SToomas Soome  * All rights reserved.
4c0455f33SToomas Soome  *
5c0455f33SToomas Soome  * Redistribution and use in source and binary forms, with or without
6c0455f33SToomas Soome  * modification, are permitted provided that the following conditions
7c0455f33SToomas Soome  * are met:
8c0455f33SToomas Soome  * 1. Redistributions of source code must retain the above copyright
9c0455f33SToomas Soome  *    notice, this list of conditions and the following disclaimer.
10c0455f33SToomas Soome  * 2. Redistributions in binary form must reproduce the above copyright
11c0455f33SToomas Soome  *    notice, this list of conditions and the following disclaimer in the
12c0455f33SToomas Soome  *    documentation and/or other materials provided with the distribution.
13c0455f33SToomas Soome  *
14c0455f33SToomas Soome  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15c0455f33SToomas Soome  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16c0455f33SToomas Soome  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17c0455f33SToomas Soome  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18c0455f33SToomas Soome  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19c0455f33SToomas Soome  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20c0455f33SToomas Soome  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21c0455f33SToomas Soome  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22c0455f33SToomas Soome  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23c0455f33SToomas Soome  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24c0455f33SToomas Soome  * SUCH DAMAGE.
25c0455f33SToomas Soome  */
26c0455f33SToomas Soome 
27c0455f33SToomas Soome #include <err.h>
28c0455f33SToomas Soome #include <getopt.h>
29c0455f33SToomas Soome #include <libgen.h>
30c0455f33SToomas Soome #include <stdbool.h>
31c0455f33SToomas Soome #include <stdio.h>
32c0455f33SToomas Soome #include <stdlib.h>
33c0455f33SToomas Soome #include <string.h>
34c0455f33SToomas Soome #include <errno.h>
35c0455f33SToomas Soome #include <sys/queue.h>
36*916bf6b9SToomas Soome #include <endian.h>
37c0455f33SToomas Soome 
38c0455f33SToomas Soome #include <sys/fs/hsfs_isospec.h>
39c0455f33SToomas Soome #include "cd9660_eltorito.h"
40c0455f33SToomas Soome 
41c0455f33SToomas Soome #include "etdump.h"
42c0455f33SToomas Soome 
43c0455f33SToomas Soome #define	ISO_DEFAULT_BLOCK_SHIFT	11
44c0455f33SToomas Soome #define	ISO_DEFAULT_BLOCK_SIZE	(1 << ISO_DEFAULT_BLOCK_SHIFT)
45c0455f33SToomas Soome 
46*916bf6b9SToomas Soome /* Little endian */
47*916bf6b9SToomas Soome void
cd9660_721(uint16_t w,unsigned char * twochar)48*916bf6b9SToomas Soome cd9660_721(uint16_t w, unsigned char *twochar)
49*916bf6b9SToomas Soome {
50*916bf6b9SToomas Soome #if BYTE_ORDER == BIG_ENDIAN
51*916bf6b9SToomas Soome 	w = htole16(w);
52*916bf6b9SToomas Soome #endif
53*916bf6b9SToomas Soome 	memcpy(twochar, &w, 2);
54*916bf6b9SToomas Soome }
55*916bf6b9SToomas Soome 
56c0455f33SToomas Soome const char *
system_id_string(uchar_t system_id)57c0455f33SToomas Soome system_id_string(uchar_t system_id)
58c0455f33SToomas Soome {
59c0455f33SToomas Soome 
60c0455f33SToomas Soome 	switch (system_id) {
61c0455f33SToomas Soome 	case ET_SYS_X86:
62c0455f33SToomas Soome 		return ("i386");
63c0455f33SToomas Soome 	case ET_SYS_PPC:
64c0455f33SToomas Soome 		return ("powerpc");
65c0455f33SToomas Soome 	case ET_SYS_MAC:
66c0455f33SToomas Soome 		return ("mac");
67c0455f33SToomas Soome 	case ET_SYS_EFI:
68c0455f33SToomas Soome 		return ("efi");
69c0455f33SToomas Soome 	default:
70c0455f33SToomas Soome 		return ("invalid");
71c0455f33SToomas Soome 	}
72c0455f33SToomas Soome }
73c0455f33SToomas Soome 
74c0455f33SToomas Soome const char *
media_type_string(uchar_t media_type)75c0455f33SToomas Soome media_type_string(uchar_t media_type)
76c0455f33SToomas Soome {
77c0455f33SToomas Soome 
78c0455f33SToomas Soome 	switch (media_type) {
79c0455f33SToomas Soome 	case ET_MEDIA_NOEM:
80c0455f33SToomas Soome 		return ("no emulation");
81c0455f33SToomas Soome 	case ET_MEDIA_12FDD:
82c0455f33SToomas Soome 		return ("1.2MB FDD");
83c0455f33SToomas Soome 	case ET_MEDIA_144FDD:
84c0455f33SToomas Soome 		return ("1.44MB FDD");
85c0455f33SToomas Soome 	case ET_MEDIA_288FDD:
86c0455f33SToomas Soome 		return ("2.88MB FDD");
87c0455f33SToomas Soome 	case ET_MEDIA_HDD:
88c0455f33SToomas Soome 		return ("HDD");
89c0455f33SToomas Soome 	default:
90c0455f33SToomas Soome 		return ("invalid");
91c0455f33SToomas Soome 	}
92c0455f33SToomas Soome }
93c0455f33SToomas Soome 
94c0455f33SToomas Soome static int
read_sector(FILE * iso,daddr_t sector,char * buffer)95c0455f33SToomas Soome read_sector(FILE *iso, daddr_t sector, char *buffer)
96c0455f33SToomas Soome {
97c0455f33SToomas Soome 
98c0455f33SToomas Soome 	fseek(iso, sector * ISO_DEFAULT_BLOCK_SIZE, SEEK_SET);
99c0455f33SToomas Soome 	if (fread(buffer, ISO_DEFAULT_BLOCK_SIZE, 1, iso) != 1) {
100c0455f33SToomas Soome 		return (errno);
101c0455f33SToomas Soome 	}
102c0455f33SToomas Soome 	return (0);
103c0455f33SToomas Soome }
104c0455f33SToomas Soome 
105c0455f33SToomas Soome static bool
boot_catalog_valid(char * entry)106c0455f33SToomas Soome boot_catalog_valid(char *entry)
107c0455f33SToomas Soome {
108c0455f33SToomas Soome 	boot_catalog_validation_entry *ve;
109c0455f33SToomas Soome 	int16_t		checksum, sum;
110c0455f33SToomas Soome 	unsigned char	*csptr;
111c0455f33SToomas Soome 	size_t		i;
112c0455f33SToomas Soome 
113c0455f33SToomas Soome 	ve = (boot_catalog_validation_entry *)entry;
114c0455f33SToomas Soome 
115c0455f33SToomas Soome 	checksum = isonum_721(ve->checksum);
116c0455f33SToomas Soome 	cd9660_721(0, ve->checksum);
117c0455f33SToomas Soome 	csptr = (unsigned char *)ve;
118c0455f33SToomas Soome 
119c0455f33SToomas Soome 	for (i = sum = 0; i < sizeof (*ve); i += 2) {
120c0455f33SToomas Soome 		sum += (int16_t)csptr[i];
121c0455f33SToomas Soome 		sum += 256 * (int16_t)csptr[i + 1];
122c0455f33SToomas Soome 	}
123c0455f33SToomas Soome 	if (sum + checksum != 0) {
124c0455f33SToomas Soome 		return (false);
125c0455f33SToomas Soome 	}
126c0455f33SToomas Soome 
127c0455f33SToomas Soome 	cd9660_721(checksum, ve->checksum);
128c0455f33SToomas Soome 	return (true);
129c0455f33SToomas Soome }
130c0455f33SToomas Soome 
131c0455f33SToomas Soome static int
dump_section(char * buffer,size_t offset,FILE * outfile,const char * filename,struct outputter * outputter)132c0455f33SToomas Soome dump_section(char *buffer, size_t offset, FILE *outfile, const char *filename,
133c0455f33SToomas Soome     struct outputter *outputter)
134c0455f33SToomas Soome {
135c0455f33SToomas Soome 	boot_catalog_section_header *sh;
136c0455f33SToomas Soome 	uchar_t platform_id;
137c0455f33SToomas Soome 	int i;
138c0455f33SToomas Soome 	size_t entry_offset;
139c0455f33SToomas Soome 	boot_catalog_section_entry *entry;
140c0455f33SToomas Soome 
141c0455f33SToomas Soome 	sh = (boot_catalog_section_header *)&buffer[offset];
142c0455f33SToomas Soome 	if (outputter->output_section != NULL) {
143c0455f33SToomas Soome 		outputter->output_section(outfile, filename, sh);
144c0455f33SToomas Soome 	}
145c0455f33SToomas Soome 
146c0455f33SToomas Soome 	platform_id = sh->platform_id[0];
147c0455f33SToomas Soome 
148c0455f33SToomas Soome 	if (outputter->output_entry != NULL) {
149c0455f33SToomas Soome 		for (i = 1; i <= (int)sh->num_section_entries[0]; i++) {
150c0455f33SToomas Soome 			entry_offset = offset + i * ET_BOOT_ENTRY_SIZE;
151c0455f33SToomas Soome 			entry =
152c0455f33SToomas Soome 			    (boot_catalog_section_entry *)&buffer[entry_offset];
153c0455f33SToomas Soome 			outputter->output_entry(outfile, filename, entry,
154c0455f33SToomas Soome 			    platform_id, false);
155c0455f33SToomas Soome 		}
156c0455f33SToomas Soome 	}
157c0455f33SToomas Soome 
158c0455f33SToomas Soome 	return (1 + (int)sh->num_section_entries[0]);
159c0455f33SToomas Soome }
160c0455f33SToomas Soome 
161c0455f33SToomas Soome static void
dump_eltorito(FILE * iso,const char * filename,FILE * outfile,struct outputter * outputter)162c0455f33SToomas Soome dump_eltorito(FILE *iso, const char *filename, FILE *outfile,
163c0455f33SToomas Soome     struct outputter *outputter)
164c0455f33SToomas Soome {
165c0455f33SToomas Soome 	char buffer[ISO_DEFAULT_BLOCK_SIZE], *entry;
166c0455f33SToomas Soome 	boot_volume_descriptor *bvd;
167c0455f33SToomas Soome 	daddr_t boot_catalog;
168c0455f33SToomas Soome 	size_t offset;
169c0455f33SToomas Soome 	int entry_count;
170c0455f33SToomas Soome 
171c0455f33SToomas Soome 	if (read_sector(iso, 17, buffer) != 0)
172c0455f33SToomas Soome 		err(1, "failed to read from image");
173c0455f33SToomas Soome 
174c0455f33SToomas Soome 	bvd = (boot_volume_descriptor *)buffer;
175c0455f33SToomas Soome 	if (memcmp(bvd->identifier, ISO_ID_STRING, 5) != 0)
176c0455f33SToomas Soome 		warnx("%s: not a valid ISO", filename);
177c0455f33SToomas Soome 	if (bvd->boot_record_indicator[0] != ISO_VD_BOOT)
178c0455f33SToomas Soome 		warnx("%s: not an El Torito bootable ISO", filename);
179c0455f33SToomas Soome 	if (memcmp(bvd->boot_system_identifier, ET_ID, 23) != 0)
180c0455f33SToomas Soome 		warnx("%s: not an El Torito bootable ISO", filename);
181c0455f33SToomas Soome 
182c0455f33SToomas Soome 	boot_catalog = isonum_731(bvd->boot_catalog_pointer);
183c0455f33SToomas Soome 
184c0455f33SToomas Soome 	if (read_sector(iso, boot_catalog, buffer) != 0)
185c0455f33SToomas Soome 		err(1, "failed to read from image");
186c0455f33SToomas Soome 
187c0455f33SToomas Soome 	entry = buffer;
188c0455f33SToomas Soome 	offset = 0;
189c0455f33SToomas Soome 
190c0455f33SToomas Soome 	if (!boot_catalog_valid(entry))
191c0455f33SToomas Soome 		warnx("%s: boot catalog checksum is invalid", filename);
192c0455f33SToomas Soome 
193c0455f33SToomas Soome 	if (outputter->output_image != NULL)
194c0455f33SToomas Soome 		outputter->output_image(outfile, filename, bvd);
195c0455f33SToomas Soome 
196c0455f33SToomas Soome 	offset += ET_BOOT_ENTRY_SIZE;
197c0455f33SToomas Soome 	entry = &buffer[offset];
198c0455f33SToomas Soome 	if (outputter->output_entry != NULL)
199c0455f33SToomas Soome 		outputter->output_entry(outfile, filename,
200c0455f33SToomas Soome 		    (boot_catalog_section_entry *)entry, 0, true);
201c0455f33SToomas Soome 
202c0455f33SToomas Soome 	offset += ET_BOOT_ENTRY_SIZE;
203c0455f33SToomas Soome 
204c0455f33SToomas Soome 	while (offset < ISO_DEFAULT_BLOCK_SIZE) {
205c0455f33SToomas Soome 		entry = &buffer[offset];
206c0455f33SToomas Soome 
207c0455f33SToomas Soome 		if ((uint8_t)entry[0] != ET_SECTION_HEADER_MORE &&
208c0455f33SToomas Soome 		    (uint8_t)entry[0] != ET_SECTION_HEADER_LAST)
209c0455f33SToomas Soome 			break;
210c0455f33SToomas Soome 
211c0455f33SToomas Soome 		entry_count = dump_section(buffer, offset, outfile, filename,
212c0455f33SToomas Soome 		    outputter);
213c0455f33SToomas Soome 
214c0455f33SToomas Soome 		offset += entry_count * ET_BOOT_ENTRY_SIZE;
215c0455f33SToomas Soome 	}
216c0455f33SToomas Soome }
217c0455f33SToomas Soome 
218c0455f33SToomas Soome static void
usage(const char * progname)219c0455f33SToomas Soome usage(const char *progname)
220c0455f33SToomas Soome {
221c0455f33SToomas Soome 	char *path;
222c0455f33SToomas Soome 
223c0455f33SToomas Soome 	path = strdup(progname);
224c0455f33SToomas Soome 
225c0455f33SToomas Soome 	fprintf(stderr, "usage: %s [-f format] [-o filename] filename [...]\n",
226c0455f33SToomas Soome 	    basename(path));
227c0455f33SToomas Soome 	fprintf(stderr, "\tsupported output formats: shell, text\n");
228c0455f33SToomas Soome 	exit(1);
229c0455f33SToomas Soome }
230c0455f33SToomas Soome 
231c0455f33SToomas Soome int
main(int argc,char ** argv)232c0455f33SToomas Soome main(int argc, char **argv)
233c0455f33SToomas Soome {
234c0455f33SToomas Soome 	int ch, i;
235c0455f33SToomas Soome 	FILE *outfile, *iso;
236c0455f33SToomas Soome 	struct outputter *outputter;
237c0455f33SToomas Soome 
238c0455f33SToomas Soome 	outfile = stdout;
239c0455f33SToomas Soome 	outputter = output_text;
240c0455f33SToomas Soome 
241c0455f33SToomas Soome 	static struct option longopts[] = {
242c0455f33SToomas Soome 		{ "format",	required_argument,	NULL,	'f' },
243c0455f33SToomas Soome 		{ "output",	required_argument,	NULL,	'o' },
244c0455f33SToomas Soome 		{ NULL,		0,			NULL,	0 },
245c0455f33SToomas Soome 	};
246c0455f33SToomas Soome 
247c0455f33SToomas Soome 	while ((ch = getopt_long(argc, argv, "f:o:", longopts, NULL)) != -1) {
248c0455f33SToomas Soome 		switch (ch) {
249c0455f33SToomas Soome 		case 'f':
250c0455f33SToomas Soome 			if (strcmp(optarg, "shell") == 0)
251c0455f33SToomas Soome 				outputter = output_shell;
252c0455f33SToomas Soome 			else if (strcmp(optarg, "text") == 0)
253c0455f33SToomas Soome 				outputter = output_text;
254c0455f33SToomas Soome 			else
255c0455f33SToomas Soome 				usage(argv[0]);
256c0455f33SToomas Soome 			break;
257c0455f33SToomas Soome 		case 'o':
258c0455f33SToomas Soome 			if (strcmp(optarg, "-") == 0) {
259c0455f33SToomas Soome 				outfile = stdout;
260c0455f33SToomas Soome 			} else if ((outfile = fopen(optarg, "w")) == NULL) {
261c0455f33SToomas Soome 				err(1, "unable to open %s for output", optarg);
262c0455f33SToomas Soome 			}
263c0455f33SToomas Soome 			break;
264c0455f33SToomas Soome 		default:
265c0455f33SToomas Soome 			usage(argv[0]);
266c0455f33SToomas Soome 		}
267c0455f33SToomas Soome 	}
268c0455f33SToomas Soome 
269c0455f33SToomas Soome 	argc -= optind;
270c0455f33SToomas Soome 	argv += optind;
271c0455f33SToomas Soome 
272c0455f33SToomas Soome 	for (i = 0; i < argc; i++) {
273c0455f33SToomas Soome 		if (strcmp(argv[i], "-") == 0) {
274c0455f33SToomas Soome 			iso = stdin;
275c0455f33SToomas Soome 		} else {
276c0455f33SToomas Soome 			iso = fopen(argv[i], "r");
277c0455f33SToomas Soome 			if (iso == NULL)
278c0455f33SToomas Soome 				err(1, "could not open %s", argv[1]);
279c0455f33SToomas Soome 		}
280c0455f33SToomas Soome 		dump_eltorito(iso, argv[i], outfile, outputter);
281c0455f33SToomas Soome 	}
282c0455f33SToomas Soome 	return (0);
283c0455f33SToomas Soome }
284