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