libinstaller: Use SOURCE_DATE_EPOCH for synthesized modification time stamps
[syslinux.git] / libinstaller / syslxmod.c
blob7bf4d91202a020c8bf8377cf6d7bdb98fd3d4a16
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 /* glibc 2.20 deprecates _BSD_SOURCE in favour of _DEFAULT_SOURCE */
21 #define _DEFAULT_SOURCE 1
22 #include <stdio.h>
23 #include <inttypes.h>
24 #include <string.h>
25 #include <stddef.h>
26 #include <stdlib.h>
28 #include "syslinux.h"
29 #include "syslxint.h"
33 * Generate sector extents
35 static void generate_extents(struct syslinux_extent _slimg *ex, int nptrs,
36 const sector_t *sectp, int nsect)
38 uint32_t addr = 0x8000; /* ldlinux.sys starts loading here */
39 uint32_t base;
40 sector_t sect, lba;
41 unsigned int len;
43 base = addr;
44 len = 0;
45 lba = 0;
47 memset_sl(ex, 0, nptrs * sizeof *ex);
49 while (nsect) {
50 sect = *sectp++;
52 if (len) {
53 uint32_t xbytes = (len + 1) * SECTOR_SIZE;
55 if (sect == lba + len && xbytes < 65536 &&
56 ((addr ^ (base + xbytes - 1)) & 0xffff0000) == 0) {
57 /* We can add to the current extent */
58 len++;
59 goto next;
62 set_64_sl(&ex->lba, lba);
63 set_16_sl(&ex->len, len);
64 ex++;
67 base = addr;
68 lba = sect;
69 len = 1;
71 next:
72 addr += SECTOR_SIZE;
73 nsect--;
76 if (len) {
77 set_64_sl(&ex->lba, lba);
78 set_16_sl(&ex->len, len);
79 ex++;
84 * Form a pointer based on a 16-bit patcharea/epa field
86 static inline void *ptr(void *img, const uint16_t _slimg *offset_p)
88 return (char *)img + get_16_sl(offset_p);
90 static inline void _slimg *slptr(void _slimg *img,
91 const uint16_t _slimg *offset_p)
93 return (char _slimg *)img + get_16_sl(offset_p);
97 * This patches the boot sector and the beginning of ldlinux.sys
98 * based on an ldlinux.sys sector map passed in. Typically this is
99 * handled by writing ldlinux.sys, mapping it, and then overwrite it
100 * with the patched version. If this isn't safe to do because of
101 * an OS which does block reallocation, then overwrite it with
102 * direct access since the location is known.
104 * Returns the number of modified bytes in ldlinux.sys if successful,
105 * otherwise -1.
107 int syslinux_patch(const sector_t *sectp, int nsectors,
108 int stupid, int raid_mode,
109 const char *subdir, const char *subvol)
111 struct patch_area _slimg *patcharea;
112 struct ext_patch_area _slimg *epa;
113 struct syslinux_extent _slimg *ex;
114 const uint32_t _slimg *wp;
115 int nsect = ((boot_image_len + SECTOR_SIZE - 1) >> SECTOR_SHIFT) + 2;
116 uint32_t csum;
117 int i, dw, nptrs;
118 struct fat_boot_sector *sbs = (struct fat_boot_sector *)boot_sector;
119 uint64_t _slimg *advptrs;
121 if (nsectors < nsect)
122 return -1; /* The actual file is too small for content */
124 /* Search for LDLINUX_MAGIC to find the patch area */
125 for (wp = (const uint32_t _slimg *)boot_image;
126 get_32_sl(wp) != LDLINUX_MAGIC;
127 wp++)
129 patcharea = (struct patch_area _slimg *)wp;
130 epa = slptr(boot_image, &patcharea->epaoffset);
132 /* First sector need pointer in boot sector */
133 set_32(ptr(sbs, &epa->sect1ptr0), sectp[0]);
134 set_32(ptr(sbs, &epa->sect1ptr1), sectp[0] >> 32);
135 sectp++;
137 /* Handle RAID mode */
138 if (raid_mode) {
139 /* Patch in INT 18h = CD 18 */
140 set_16(ptr(sbs, &epa->raidpatch), 0x18CD);
143 /* Set up the totals */
144 dw = boot_image_len >> 2; /* COMPLETE dwords, excluding ADV */
145 set_16_sl(&patcharea->data_sectors, nsect - 2); /* Not including ADVs */
146 set_16_sl(&patcharea->adv_sectors, 2); /* ADVs need 2 sectors */
147 set_32_sl(&patcharea->dwords, dw);
149 /* Handle Stupid mode */
150 if (stupid) {
151 /* Access only one sector at a time */
152 set_16_sl(&patcharea->maxtransfer, 1);
155 /* Set the sector extents */
156 ex = slptr(boot_image, &epa->secptroffset);
157 nptrs = get_16_sl(&epa->secptrcnt);
159 #if 0
160 if (nsect > nptrs) {
161 /* Not necessarily an error in this case, but a general problem */
162 fprintf(stderr, "Insufficient extent space, build error!\n");
163 exit(1);
165 #endif
167 /* -1 for the pointer in the boot sector, -2 for the two ADVs */
168 generate_extents(ex, nptrs, sectp, nsect-1-2);
170 /* ADV pointers */
171 advptrs = slptr(boot_image, &epa->advptroffset);
172 set_64_sl(&advptrs[0], sectp[nsect-1-2]);
173 set_64_sl(&advptrs[1], sectp[nsect-1-1]);
175 /* Poke in the base directory path */
176 if (subdir) {
177 int sublen = strlen(subdir) + 1;
178 if (get_16_sl(&epa->dirlen) < sublen) {
179 fprintf(stderr, "Subdirectory path too long... aborting install!\n");
180 exit(1);
182 memcpy_to_sl(slptr(boot_image, &epa->diroffset), subdir, sublen);
185 /* Poke in the subvolume information */
186 if (subvol) {
187 int sublen = strlen(subvol) + 1;
188 if (get_16_sl(&epa->subvollen) < sublen) {
189 fprintf(stderr, "Subvol name too long... aborting install!\n");
190 exit(1);
192 memcpy_to_sl(slptr(boot_image, &epa->subvoloffset), subvol, sublen);
195 /* Now produce a checksum */
196 set_32_sl(&patcharea->checksum, 0);
198 csum = LDLINUX_MAGIC;
199 for (i = 0, wp = (const uint32_t _slimg *)boot_image; i < dw; i++, wp++)
200 csum -= get_32_sl(wp); /* Negative checksum */
202 set_32_sl(&patcharea->checksum, csum);
205 * Assume all bytes modified. This can be optimized at the expense
206 * of keeping track of what the highest modified address ever was.
208 return dw << 2;