Revert "ldlinux: Allow a held shift key to override UI as well as DEFAULT"
[syslinux/sherbszt.git] / libinstaller / syslxmod.c
blobcc6e56a2b96f6aea16a8dd6e336790336a7889e4
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1998-2008 H. Peter Anvin - All Rights Reserved
4 * Copyright 2009-2014 Intel Corporation; author H. Peter Anvin
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
9 * Boston MA 02111-1307, USA; either version 2 of the License, or
10 * (at your option) any later version; incorporated herein by reference.
12 * ----------------------------------------------------------------------- */
15 * syslxmod.c - Code to provide a SYSLINUX code set to an installer.
18 #define _XOPEN_SOURCE 500 /* Required on glibc 2.x */
19 #define _BSD_SOURCE
20 #include <stdio.h>
21 #include <inttypes.h>
22 #include <string.h>
23 #include <stddef.h>
24 #include <stdlib.h>
26 #include "syslinux.h"
27 #include "syslxint.h"
31 * Generate sector extents
33 static void generate_extents(struct syslinux_extent _slimg *ex, int nptrs,
34 const sector_t *sectp, int nsect)
36 uint32_t addr = 0x8000; /* ldlinux.sys starts loading here */
37 uint32_t base;
38 sector_t sect, lba;
39 unsigned int len;
41 base = addr;
42 len = lba = 0;
44 memset_sl(ex, 0, nptrs * sizeof *ex);
46 while (nsect) {
47 sect = *sectp++;
49 if (len) {
50 uint32_t xbytes = (len + 1) * SECTOR_SIZE;
52 if (sect == lba + len && xbytes < 65536 &&
53 ((addr ^ (base + xbytes - 1)) & 0xffff0000) == 0) {
54 /* We can add to the current extent */
55 len++;
56 goto next;
59 set_64_sl(&ex->lba, lba);
60 set_16_sl(&ex->len, len);
61 ex++;
64 base = addr;
65 lba = sect;
66 len = 1;
68 next:
69 addr += SECTOR_SIZE;
70 nsect--;
73 if (len) {
74 set_64_sl(&ex->lba, lba);
75 set_16_sl(&ex->len, len);
76 ex++;
81 * Form a pointer based on a 16-bit patcharea/epa field
83 static inline void *ptr(void *img, const uint16_t _slimg *offset_p)
85 return (char *)img + get_16_sl(offset_p);
87 static inline void _slimg *slptr(void _slimg *img,
88 const uint16_t _slimg *offset_p)
90 return (char _slimg *)img + get_16_sl(offset_p);
94 * This patches the boot sector and the beginning of ldlinux.sys
95 * based on an ldlinux.sys sector map passed in. Typically this is
96 * handled by writing ldlinux.sys, mapping it, and then overwrite it
97 * with the patched version. If this isn't safe to do because of
98 * an OS which does block reallocation, then overwrite it with
99 * direct access since the location is known.
101 * Returns the number of modified bytes in ldlinux.sys if successful,
102 * otherwise -1.
104 int syslinux_patch(const sector_t *sectp, int nsectors,
105 int stupid, int raid_mode,
106 const char *subdir, const char *subvol)
108 struct patch_area _slimg *patcharea;
109 struct ext_patch_area _slimg *epa;
110 struct syslinux_extent _slimg *ex;
111 const uint32_t _slimg *wp;
112 int nsect = ((boot_image_len + SECTOR_SIZE - 1) >> SECTOR_SHIFT) + 2;
113 uint32_t csum;
114 int i, dw, nptrs;
115 struct fat_boot_sector *sbs = (struct fat_boot_sector *)boot_sector;
116 uint64_t _slimg *advptrs;
118 if (nsectors < nsect)
119 return -1; /* The actual file is too small for content */
121 /* Search for LDLINUX_MAGIC to find the patch area */
122 for (wp = (const uint32_t _slimg *)boot_image;
123 get_32_sl(wp) != LDLINUX_MAGIC;
124 wp++)
126 patcharea = (struct patch_area _slimg *)wp;
127 epa = slptr(boot_image, &patcharea->epaoffset);
129 /* First sector need pointer in boot sector */
130 set_32(ptr(sbs, &epa->sect1ptr0), sectp[0]);
131 set_32(ptr(sbs, &epa->sect1ptr1), sectp[0] >> 32);
132 sectp++;
134 /* Handle RAID mode */
135 if (raid_mode) {
136 /* Patch in INT 18h = CD 18 */
137 set_16(ptr(sbs, &epa->raidpatch), 0x18CD);
140 /* Set up the totals */
141 dw = boot_image_len >> 2; /* COMPLETE dwords, excluding ADV */
142 set_16_sl(&patcharea->data_sectors, nsect - 2); /* Not including ADVs */
143 set_16_sl(&patcharea->adv_sectors, 2); /* ADVs need 2 sectors */
144 set_32_sl(&patcharea->dwords, dw);
146 /* Handle Stupid mode */
147 if (stupid) {
148 /* Access only one sector at a time */
149 set_16_sl(&patcharea->maxtransfer, 1);
152 /* Set the sector extents */
153 ex = slptr(boot_image, &epa->secptroffset);
154 nptrs = get_16_sl(&epa->secptrcnt);
156 #if 0
157 if (nsect > nptrs) {
158 /* Not necessarily an error in this case, but a general problem */
159 fprintf(stderr, "Insufficient extent space, build error!\n");
160 exit(1);
162 #endif
164 /* -1 for the pointer in the boot sector, -2 for the two ADVs */
165 generate_extents(ex, nptrs, sectp, nsect-1-2);
167 /* ADV pointers */
168 advptrs = slptr(boot_image, &epa->advptroffset);
169 set_64_sl(&advptrs[0], sectp[nsect-1-2]);
170 set_64_sl(&advptrs[1], sectp[nsect-1-1]);
172 /* Poke in the base directory path */
173 if (subdir) {
174 int sublen = strlen(subdir) + 1;
175 if (get_16_sl(&epa->dirlen) < sublen) {
176 fprintf(stderr, "Subdirectory path too long... aborting install!\n");
177 exit(1);
179 memcpy_to_sl(slptr(boot_image, &epa->diroffset), subdir, sublen);
182 /* Poke in the subvolume information */
183 if (subvol) {
184 int sublen = strlen(subvol) + 1;
185 if (get_16_sl(&epa->subvollen) < sublen) {
186 fprintf(stderr, "Subvol name too long... aborting install!\n");
187 exit(1);
189 memcpy_to_sl(slptr(boot_image, &epa->subvoloffset), subvol, sublen);
192 /* Now produce a checksum */
193 set_32_sl(&patcharea->checksum, 0);
195 csum = LDLINUX_MAGIC;
196 for (i = 0, wp = (const uint32_t _slimg *)boot_image; i < dw; i++, wp++)
197 csum -= get_32_sl(wp); /* Negative checksum */
199 set_32_sl(&patcharea->checksum, csum);
202 * Assume all bytes modified. This can be optimized at the expense
203 * of keeping track of what the highest modified address ever was.
205 return dw << 2;