[e16e8f2] | 1 | /* ----------------------------------------------------------------------- * |
---|
| 2 | * |
---|
| 3 | * Copyright 1998-2008 H. Peter Anvin - All Rights Reserved |
---|
| 4 | * Copyright 2009-2014 Intel Corporation; author H. Peter Anvin |
---|
| 5 | * |
---|
| 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. |
---|
| 11 | * |
---|
| 12 | * ----------------------------------------------------------------------- */ |
---|
| 13 | |
---|
| 14 | /* |
---|
| 15 | * syslxmod.c - Code to provide a SYSLINUX code set to an installer. |
---|
| 16 | */ |
---|
| 17 | |
---|
| 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> |
---|
| 27 | |
---|
| 28 | #include "syslinux.h" |
---|
| 29 | #include "syslxint.h" |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | /* |
---|
| 33 | * Generate sector extents |
---|
| 34 | */ |
---|
| 35 | static void generate_extents(struct syslinux_extent _slimg *ex, int nptrs, |
---|
| 36 | const sector_t *sectp, int nsect) |
---|
| 37 | { |
---|
| 38 | uint32_t addr = 0x8000; /* ldlinux.sys starts loading here */ |
---|
| 39 | uint32_t base; |
---|
| 40 | sector_t sect, lba; |
---|
| 41 | unsigned int len; |
---|
| 42 | |
---|
| 43 | base = addr; |
---|
| 44 | len = lba = 0; |
---|
| 45 | |
---|
| 46 | memset_sl(ex, 0, nptrs * sizeof *ex); |
---|
| 47 | |
---|
| 48 | while (nsect) { |
---|
| 49 | sect = *sectp++; |
---|
| 50 | |
---|
| 51 | if (len) { |
---|
| 52 | uint32_t xbytes = (len + 1) * SECTOR_SIZE; |
---|
| 53 | |
---|
| 54 | if (sect == lba + len && xbytes < 65536 && |
---|
| 55 | ((addr ^ (base + xbytes - 1)) & 0xffff0000) == 0) { |
---|
| 56 | /* We can add to the current extent */ |
---|
| 57 | len++; |
---|
| 58 | goto next; |
---|
| 59 | } |
---|
| 60 | |
---|
| 61 | set_64_sl(&ex->lba, lba); |
---|
| 62 | set_16_sl(&ex->len, len); |
---|
| 63 | ex++; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | base = addr; |
---|
| 67 | lba = sect; |
---|
| 68 | len = 1; |
---|
| 69 | |
---|
| 70 | next: |
---|
| 71 | addr += SECTOR_SIZE; |
---|
| 72 | nsect--; |
---|
| 73 | } |
---|
| 74 | |
---|
| 75 | if (len) { |
---|
| 76 | set_64_sl(&ex->lba, lba); |
---|
| 77 | set_16_sl(&ex->len, len); |
---|
| 78 | ex++; |
---|
| 79 | } |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | /* |
---|
| 83 | * Form a pointer based on a 16-bit patcharea/epa field |
---|
| 84 | */ |
---|
| 85 | static inline void *ptr(void *img, const uint16_t _slimg *offset_p) |
---|
| 86 | { |
---|
| 87 | return (char *)img + get_16_sl(offset_p); |
---|
| 88 | } |
---|
| 89 | static inline void _slimg *slptr(void _slimg *img, |
---|
| 90 | const uint16_t _slimg *offset_p) |
---|
| 91 | { |
---|
| 92 | return (char _slimg *)img + get_16_sl(offset_p); |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | /* |
---|
| 96 | * This patches the boot sector and the beginning of ldlinux.sys |
---|
| 97 | * based on an ldlinux.sys sector map passed in. Typically this is |
---|
| 98 | * handled by writing ldlinux.sys, mapping it, and then overwrite it |
---|
| 99 | * with the patched version. If this isn't safe to do because of |
---|
| 100 | * an OS which does block reallocation, then overwrite it with |
---|
| 101 | * direct access since the location is known. |
---|
| 102 | * |
---|
| 103 | * Returns the number of modified bytes in ldlinux.sys if successful, |
---|
| 104 | * otherwise -1. |
---|
| 105 | */ |
---|
| 106 | int syslinux_patch(const sector_t *sectp, int nsectors, |
---|
| 107 | int stupid, int raid_mode, |
---|
| 108 | const char *subdir, const char *subvol) |
---|
| 109 | { |
---|
| 110 | struct patch_area _slimg *patcharea; |
---|
| 111 | struct ext_patch_area _slimg *epa; |
---|
| 112 | struct syslinux_extent _slimg *ex; |
---|
| 113 | const uint32_t _slimg *wp; |
---|
| 114 | int nsect = ((boot_image_len + SECTOR_SIZE - 1) >> SECTOR_SHIFT) + 2; |
---|
| 115 | uint32_t csum; |
---|
| 116 | int i, dw, nptrs; |
---|
| 117 | struct fat_boot_sector *sbs = (struct fat_boot_sector *)boot_sector; |
---|
| 118 | uint64_t _slimg *advptrs; |
---|
| 119 | |
---|
| 120 | if (nsectors < nsect) |
---|
| 121 | return -1; /* The actual file is too small for content */ |
---|
| 122 | |
---|
| 123 | /* Search for LDLINUX_MAGIC to find the patch area */ |
---|
| 124 | for (wp = (const uint32_t _slimg *)boot_image; |
---|
| 125 | get_32_sl(wp) != LDLINUX_MAGIC; |
---|
| 126 | wp++) |
---|
| 127 | ; |
---|
| 128 | patcharea = (struct patch_area _slimg *)wp; |
---|
| 129 | epa = slptr(boot_image, &patcharea->epaoffset); |
---|
| 130 | |
---|
| 131 | /* First sector need pointer in boot sector */ |
---|
| 132 | set_32(ptr(sbs, &epa->sect1ptr0), sectp[0]); |
---|
| 133 | set_32(ptr(sbs, &epa->sect1ptr1), sectp[0] >> 32); |
---|
| 134 | sectp++; |
---|
| 135 | |
---|
| 136 | /* Handle RAID mode */ |
---|
| 137 | if (raid_mode) { |
---|
| 138 | /* Patch in INT 18h = CD 18 */ |
---|
| 139 | set_16(ptr(sbs, &epa->raidpatch), 0x18CD); |
---|
| 140 | } |
---|
| 141 | |
---|
| 142 | /* Set up the totals */ |
---|
| 143 | dw = boot_image_len >> 2; /* COMPLETE dwords, excluding ADV */ |
---|
| 144 | set_16_sl(&patcharea->data_sectors, nsect - 2); /* Not including ADVs */ |
---|
| 145 | set_16_sl(&patcharea->adv_sectors, 2); /* ADVs need 2 sectors */ |
---|
| 146 | set_32_sl(&patcharea->dwords, dw); |
---|
| 147 | |
---|
| 148 | /* Handle Stupid mode */ |
---|
| 149 | if (stupid) { |
---|
| 150 | /* Access only one sector at a time */ |
---|
| 151 | set_16_sl(&patcharea->maxtransfer, 1); |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | /* Set the sector extents */ |
---|
| 155 | ex = slptr(boot_image, &epa->secptroffset); |
---|
| 156 | nptrs = get_16_sl(&epa->secptrcnt); |
---|
| 157 | |
---|
| 158 | #if 0 |
---|
| 159 | if (nsect > nptrs) { |
---|
| 160 | /* Not necessarily an error in this case, but a general problem */ |
---|
| 161 | fprintf(stderr, "Insufficient extent space, build error!\n"); |
---|
| 162 | exit(1); |
---|
| 163 | } |
---|
| 164 | #endif |
---|
| 165 | |
---|
| 166 | /* -1 for the pointer in the boot sector, -2 for the two ADVs */ |
---|
| 167 | generate_extents(ex, nptrs, sectp, nsect-1-2); |
---|
| 168 | |
---|
| 169 | /* ADV pointers */ |
---|
| 170 | advptrs = slptr(boot_image, &epa->advptroffset); |
---|
| 171 | set_64_sl(&advptrs[0], sectp[nsect-1-2]); |
---|
| 172 | set_64_sl(&advptrs[1], sectp[nsect-1-1]); |
---|
| 173 | |
---|
| 174 | /* Poke in the base directory path */ |
---|
| 175 | if (subdir) { |
---|
| 176 | int sublen = strlen(subdir) + 1; |
---|
| 177 | if (get_16_sl(&epa->dirlen) < sublen) { |
---|
| 178 | fprintf(stderr, "Subdirectory path too long... aborting install!\n"); |
---|
| 179 | exit(1); |
---|
| 180 | } |
---|
| 181 | memcpy_to_sl(slptr(boot_image, &epa->diroffset), subdir, sublen); |
---|
| 182 | } |
---|
| 183 | |
---|
| 184 | /* Poke in the subvolume information */ |
---|
| 185 | if (subvol) { |
---|
| 186 | int sublen = strlen(subvol) + 1; |
---|
| 187 | if (get_16_sl(&epa->subvollen) < sublen) { |
---|
| 188 | fprintf(stderr, "Subvol name too long... aborting install!\n"); |
---|
| 189 | exit(1); |
---|
| 190 | } |
---|
| 191 | memcpy_to_sl(slptr(boot_image, &epa->subvoloffset), subvol, sublen); |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | /* Now produce a checksum */ |
---|
| 195 | set_32_sl(&patcharea->checksum, 0); |
---|
| 196 | |
---|
| 197 | csum = LDLINUX_MAGIC; |
---|
| 198 | for (i = 0, wp = (const uint32_t _slimg *)boot_image; i < dw; i++, wp++) |
---|
| 199 | csum -= get_32_sl(wp); /* Negative checksum */ |
---|
| 200 | |
---|
| 201 | set_32_sl(&patcharea->checksum, csum); |
---|
| 202 | |
---|
| 203 | /* |
---|
| 204 | * Assume all bytes modified. This can be optimized at the expense |
---|
| 205 | * of keeping track of what the highest modified address ever was. |
---|
| 206 | */ |
---|
| 207 | return dw << 2; |
---|
| 208 | } |
---|