1From 5a2d571f3687910260c45841725f2deb84c8f12e Mon Sep 17 00:00:00 2001
2From: Hongxu Jia <hongxu.jia@windriver.com>
3Date: Mon, 25 Apr 2022 18:18:00 +0800
4Subject: [PATCH] add new option -eltorito-platform
5
6Mkisofs now correctly supports El Torito multi boot entries by introducing
7a Boot Dection Header before a list of alternate boot entries.
8
9New option -eltorito-platform allows to set the El Torito platform id
10for a boot entry or for a list of boot entries. Supported values for
11the parameter are:
12-   x86 the standard value vor x86 based PCs
13-   PPC the Power PC platform
14-   Mac The Apple Mac platform
15-   efi EFI based boot for PCs
16-   #   an arbitrary numerical value
17
18Upstream-Status: Inappropriate [port from cdrtools]
19https://github.com/jobermayr/cdrtools/commit/a50804fd61d75eb689a515dbfca6968ca2296fd7
20
21Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com>
22---
23 genisoimage/eltorito.c    | 73 +++++++++++++++++++++++++++++++++++++--
24 genisoimage/genisoimage.c | 47 +++++++++++++++++++++++++
25 genisoimage/genisoimage.h |  8 +++++
26 genisoimage/iso9660.h     | 33 ++++++++++++++++--
27 4 files changed, 157 insertions(+), 4 deletions(-)
28
29diff --git a/genisoimage/eltorito.c b/genisoimage/eltorito.c
30index d52e17e..a804988 100644
31--- a/genisoimage/eltorito.c
32+++ b/genisoimage/eltorito.c
33@@ -56,6 +56,7 @@ static unsigned int bcat_de_flags;
34 void	init_boot_catalog(const char *path);
35 void	insert_boot_cat(void);
36 static	void	get_torito_desc(struct eltorito_boot_descriptor *boot_desc);
37+static	void	fill_boot_shdr(struct eltorito_sectionheader_entry *boot_shdr_entry, int arch);
38 static	void	fill_boot_desc(struct eltorito_defaultboot_entry *boot_desc_entry,
39 										struct eltorito_boot_entry_info *boot_entry);
40 void	get_boot_entry(void);
41@@ -282,7 +283,14 @@ get_torito_desc(struct eltorito_boot_descriptor *boot_desc)
42 	struct directory_entry	*de2;	/* Boot catalog */
43 	int			i;
44 	int			offset;
45+	int			arch = 0;
46+	int			nentries = 0;
47 	struct eltorito_defaultboot_entry boot_desc_record;
48+	struct eltorito_sectionheader_entry boot_shdr_record;
49+#ifdef __needed__
50+	struct eltorito_section_entry boot_section_record;
51+#endif
52+	struct eltorito_sectionheader_entry *last_section_header = 0;
53
54 	memset(boot_desc, 0, sizeof (*boot_desc));
55 	boot_desc->type[0] = 0;
56@@ -311,13 +319,22 @@ get_torito_desc(struct eltorito_boot_descriptor *boot_desc)
57 	set_731(boot_desc->bootcat_ptr,
58 		(unsigned int) get_733(de2->isorec.extent));
59
60+	/*
61+	 * If the platform id for the first (default) boot entry has not been
62+	 * explicitly set, we default to EL_TORITO_ARCH_x86
63+	 */
64+	if ((first_boot_entry->type & ELTORITO_BOOT_ID) == 0) {
65+		first_boot_entry->boot_platform = EL_TORITO_ARCH_x86;
66+	}
67+	arch = first_boot_entry->boot_platform;
68+
69 	/*
70 	 * we have the boot image, so write boot catalog information
71 	 * Next we write out the primary descriptor for the disc
72 	 */
73 	memset(&valid_desc, 0, sizeof (valid_desc));
74 	valid_desc.headerid[0] = 1;
75-	valid_desc.arch[0] = EL_TORITO_ARCH_x86;
76+	valid_desc.arch[0] = arch;  /* Platform id for the default boot */
77
78 	/*
79 	 * we'll shove start of publisher id into id field,
80@@ -351,8 +368,17 @@ get_torito_desc(struct eltorito_boot_descriptor *boot_desc)
81 		current_boot_entry != NULL;
82 		current_boot_entry = current_boot_entry->next,
83 		offset += sizeof (boot_desc_record)) {
84+		int newarch = arch;
85
86-		if (offset >= SECTOR_SIZE) {
87+		if (current_boot_entry->type & ELTORITO_BOOT_ID)
88+			newarch = current_boot_entry->boot_platform;
89+		else
90+			current_boot_entry->boot_platform = arch;
91+
92+		/*
93+		 * El Torito has no such limitation but we currently have...
94+		 */
95+		if (offset >= (SECTOR_SIZE - sizeof (boot_desc_record))) {
96 #ifdef	USE_LIBSCHILY
97 			comerrno(EX_BAD,
98 			"Too many El Torito boot entries\n");
99@@ -362,12 +388,53 @@ get_torito_desc(struct eltorito_boot_descriptor *boot_desc)
100 			exit(1);
101 #endif
102 		}
103+
104+		if (current_boot_entry == first_boot_entry) {
105+			;
106+			/* EMPTY */
107+		} else if ((current_boot_entry == first_boot_entry->next) ||
108+			    (arch != newarch) ||
109+			    (current_boot_entry->type & ELTORITO_SECTION_HEADER)) {
110+			if (last_section_header)
111+				set_721(&last_section_header->entry_count, nentries);
112+			nentries = 1;
113+			last_section_header = (struct eltorito_sectionheader_entry *)
114+							(de2->table + offset);
115+			fill_boot_shdr(&boot_shdr_record, newarch);
116+			memcpy(de2->table + offset, &boot_shdr_record,
117+						sizeof (boot_shdr_record));
118+			offset += sizeof (boot_desc_record);
119+		} else {
120+			nentries++; /* Add entry to this section header */
121+		}
122+		/*
123+		 * This works because a section entry has the same essential
124+		 * layout as a default entry (and we do not populate the
125+		 * selection criteria fields).
126+		 */
127+
128 		fill_boot_desc(&boot_desc_record, current_boot_entry);
129 		memcpy(de2->table + offset, &boot_desc_record,
130 					sizeof (boot_desc_record));
131 	}
132+
133+	if (last_section_header) {
134+		set_721(&last_section_header->entry_count, nentries);
135+		last_section_header->header_id[0] = EL_TORITO_SHDR_ID_LAST_SHDR;
136+	}
137+
138 }/* get_torito_desc(... */
139
140+static void
141+fill_boot_shdr(boot_shdr_entry, arch)
142+	struct eltorito_sectionheader_entry *boot_shdr_entry;
143+	int                 arch;
144+{
145+	memset(boot_shdr_entry, 0, sizeof(struct eltorito_sectionheader_entry));
146+	boot_shdr_entry->header_id[0] = EL_TORITO_SHDR_ID_SHDR;
147+	boot_shdr_entry->platform_id[0] = arch;
148+}
149+
150 static void
151 fill_boot_desc(struct eltorito_defaultboot_entry *boot_desc_entry,
152 					struct eltorito_boot_entry_info *boot_entry)
153@@ -678,7 +745,9 @@ get_boot_entry()
154 	if (!first_boot_entry) {
155 		first_boot_entry = current_boot_entry;
156 		last_boot_entry = current_boot_entry;
157+		current_boot_entry->boot_platform = EL_TORITO_ARCH_x86;
158 	} else {
159+		current_boot_entry->boot_platform = last_boot_entry->boot_platform;
160 		last_boot_entry->next = current_boot_entry;
161 		last_boot_entry = current_boot_entry;
162 	}
163diff --git a/genisoimage/genisoimage.c b/genisoimage/genisoimage.c
164index 9089081..84ac3c2 100644
165--- a/genisoimage/genisoimage.c
166+++ b/genisoimage/genisoimage.c
167@@ -271,6 +271,8 @@ struct rcopts {
168 	char		**variable;
169 };
170
171+static int get_boot_platid(char *opt_arg);
172+
173 struct rcopts rcopt[] = {
174 	{"PREP", &preparer},
175 	{"PUBL", &publisher},
176@@ -404,6 +406,7 @@ struct ld_option {
177
178 #define	OPTION_ALLOW_LEADING_DOTS	1070
179 #define	OPTION_PUBLISHER		1071
180+#define	OPTION_PLATFORM			1072
181
182 #ifdef		JIGDO_TEMPLATE
183 #define	OPTION_JTT_OUTPUT		1101
184@@ -528,6 +531,8 @@ static const struct ld_option ld_options[] =
185 	'b', "FILE", "Set El Torito boot image name", ONE_DASH},
186 	{{"eltorito-alt-boot", no_argument, NULL, OPTION_ALT_BOOT},
187 	'\0', NULL, "Start specifying alternative El Torito boot parameters", ONE_DASH},
188+	{{"eltorito-platform", required_argument, NULL, OPTION_PLATFORM},
189+	'\0', "ID", "Set El Torito platform id for the next boot entry", ONE_DASH},
190 	{{"sparc-boot", required_argument, NULL, 'B'},
191 	'B', "FILES", "Set sparc boot image names", ONE_DASH},
192 	{{"sunx86-boot", required_argument, NULL, OPTION_SUNX86BOOT},
193@@ -1558,6 +1563,9 @@ int main(int argc, char *argv[])
194 			 */
195 			new_boot_entry();
196 			break;
197+		case OPTION_PLATFORM:
198+			get_boot_platid(optarg);
199+			break;
200 		case OPTION_BOOTALPHA:
201 			use_alphaboot++;
202 			/* list of pathnames of boot images */
203@@ -3829,3 +3837,42 @@ e_malloc(size_t size)
204      memset(pt, 0, size);
205 	return (pt);
206 }
207+
208+static int
209+get_boot_platid(char *opt_arg)
210+{
211+	long    val;
212+	char    *ptr;
213+
214+	use_eltorito++;
215+	if (streql(opt_arg, "x86")) {
216+		val = EL_TORITO_ARCH_x86;
217+	} else if (streql(opt_arg, "PPC")) {
218+		val = EL_TORITO_ARCH_PPC;
219+	} else if (streql(opt_arg, "Mac")) {
220+		val = EL_TORITO_ARCH_PPC;
221+	} else if (streql(opt_arg, "efi")) {
222+		val = EL_TORITO_ARCH_EFI;
223+	} else {
224+		val = strtol(opt_arg, &ptr, 0);
225+		if (*ptr || val < 0 || val >= 0x100) {
226+			comerrno(EX_BAD, "Bad boot system ID.\n");
227+		}
228+	}
229+
230+	/*
231+	 * If there is already a boot entry and the boot file name has been set
232+	 * for this boot entry and the new platform id differs from the
233+	 * previous value, we start a new boot section.
234+	 */
235+	if (current_boot_entry &&
236+	    current_boot_entry->boot_image != NULL &&
237+	    current_boot_entry->boot_platform != val) {
238+	    new_boot_entry();
239+	}
240+	get_boot_entry();
241+	current_boot_entry->type |= ELTORITO_BOOT_ID;
242+	current_boot_entry->boot_platform = val;
243+	return (1);
244+}
245+
246diff --git a/genisoimage/genisoimage.h b/genisoimage/genisoimage.h
247index 82c859b..1170d89 100644
248--- a/genisoimage/genisoimage.h
249+++ b/genisoimage/genisoimage.h
250@@ -299,6 +299,14 @@ struct eltorito_boot_entry_info {
251 	int		boot_info_table;
252 	int		load_size;
253 	int		load_addr;
254+
255+#define	ELTORITO_BOOT_ID    1
256+#define	ELTORITO_SECTION_HEADER 2
257+	int     type;
258+	/*
259+	 * Valid if (type & ELTORITO_BOOT_ID) != 0
260+	 */
261+	int     boot_platform;
262 };
263
264 extern int	goof;
265diff --git a/genisoimage/iso9660.h b/genisoimage/iso9660.h
266index c74c2a9..61b6fc0 100644
267--- a/genisoimage/iso9660.h
268+++ b/genisoimage/iso9660.h
269@@ -62,6 +62,7 @@ struct iso_volume_descriptor {
270 #define	EL_TORITO_ARCH_x86	0
271 #define	EL_TORITO_ARCH_PPC	1
272 #define	EL_TORITO_ARCH_MAC	2
273+#define	EL_TORITO_ARCH_EFI	0xEF
274
275 #define	EL_TORITO_BOOTABLE	0x88
276 #define	EL_TORITO_NOT_BOOTABLE	0
277@@ -159,10 +160,15 @@ struct eltorito_boot_descriptor {
278 };
279
280 /* Validation entry for El Torito */
281+/*
282+ * headerid must be 1
283+ * id is the manufacturer ID
284+ * cksum to make the sum of all shorts in this record 0
285+ */
286 struct eltorito_validation_entry {
287 	char headerid			[ISODCL(1,    1)]; /* 711 */
288 	char arch			[ISODCL(2,    2)];
289-	char pad1			[ISODCL(3,    4)]; /* 711 */
290+	char pad1			[ISODCL(3,    4)]; /* 721 */
291 	char id				[ISODCL(5,   28)]; /* CD devel/man*/
292 	char cksum			[ISODCL(29,  30)];
293 	char key1			[ISODCL(31,  31)];
294@@ -173,7 +179,7 @@ struct eltorito_validation_entry {
295 struct eltorito_defaultboot_entry {
296 	char boot_id			[ISODCL(1,    1)]; /* 711 */
297 	char boot_media			[ISODCL(2,    2)];
298-	char loadseg			[ISODCL(3,    4)]; /* 711 */
299+	char loadseg			[ISODCL(3,    4)]; /* 721 */
300 	char sys_type			[ISODCL(5,    5)];
301 	char pad1			[ISODCL(6,    6)];
302 	char nsect			[ISODCL(7,    8)];
303@@ -181,6 +187,29 @@ struct eltorito_defaultboot_entry {
304 	char pad2			[ISODCL(13,  32)];
305 };
306
307+/* El Torito section header entry in boot catalog */
308+struct eltorito_sectionheader_entry {
309+#define    EL_TORITO_SHDR_ID_SHDR      0x90
310+#define    EL_TORITO_SHDR_ID_LAST_SHDR 0x91
311+	char header_id          [ISODCL(1,    1)]; /* 711 */
312+	char platform_id        [ISODCL(2,    2)];
313+	char entry_count        [ISODCL(3,    4)]; /* 721 */
314+	char id             [ISODCL(5,   32)];
315+};
316+
317+/* El Torito section entry in boot catalog */
318+struct eltorito_section_entry {
319+	char boot_id            [ISODCL(1,    1)]; /* 711 */
320+	char boot_media         [ISODCL(2,    2)];
321+	char loadseg            [ISODCL(3,    4)]; /* 721 */
322+	char sys_type           [ISODCL(5,    5)];
323+	char pad1           [ISODCL(6,    6)];
324+	char nsect          [ISODCL(7,    8)];
325+	char bootoff            [ISODCL(9,   12)];
326+	char sel_criteria       [ISODCL(13,  13)];
327+	char vendor_sel_criteria    [ISODCL(14,  32)];
328+};
329+
330 /*
331  * XXX JS: The next two structures have odd lengths!
332  * Some compilers (e.g. on Sun3/mc68020) padd the structures to even length.
333--
3342.27.0
335
336