Releasing debian version 4.06+dfsg-2.
[syslinux-debian/hramrach.git] / libinstaller / syslxint.h
blobe5428b79f0f180677c76a56e4678736b60d9b722
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
4 * Copyright 2009-2011 Intel Corporation; author: H. Peter Anvin
5 * Copyright 2011 Paulo Alcantara <pcacjr@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, Inc., 53 Temple Place Ste 330,
10 * Boston MA 02111-1307, USA; either version 2 of the License, or
11 * (at your option) any later version; incorporated herein by reference.
13 * ----------------------------------------------------------------------- */
15 #ifndef SYSLXINT_H
16 #define SYSLXINT_H
18 #include "syslinux.h"
20 #if defined(__386__) || defined(__i386__) || defined(__x86_64__)
21 # define X86_MEM 1 /* Littleendian and unaligned safe */
22 #else
23 # define X86_MEM 0
24 #endif
26 #ifdef __GNUC__
27 # ifdef __MINGW32__
28 /* gcc 4.7 miscompiles packed structures in MS-bitfield mode */
29 # define PACKED __attribute__((packed,gcc_struct))
30 # else
31 # define PACKED __attribute__((packed))
32 # endif
33 #else
34 # error "Need to define PACKED for this compiler"
35 #endif
38 * Access functions for littleendian numbers, possibly misaligned.
40 static inline uint8_t get_8(const uint8_t * p)
42 return *p;
45 static inline uint16_t get_16(const uint16_t * p)
47 #if X86_MEM
48 /* Littleendian and unaligned-capable */
49 return *p;
50 #else
51 const uint8_t *pp = (const uint8_t *)p;
52 return pp[0] + ((uint16_t)pp[1] << 8);
53 #endif
56 static inline uint32_t get_32(const uint32_t * p)
58 #if X86_MEM
59 /* Littleendian and unaligned-capable */
60 return *p;
61 #else
62 const uint16_t *pp = (const uint16_t *)p;
63 return get_16(pp[0]) + (uint32_t)get_16(pp[1]);
64 #endif
67 static inline uint64_t get_64(const uint64_t * p)
69 #if X86_MEM
70 /* Littleendian and unaligned-capable */
71 return *p;
72 #else
73 const uint32_t *pp = (const uint32_t *)p;
74 return get_32(pp[0]) + (uint64_t)get_32(pp[1]);
75 #endif
78 static inline void set_8(uint8_t *p, uint8_t v)
80 *p = v;
83 static inline void set_16(uint16_t *p, uint16_t v)
85 #if X86_MEM
86 /* Littleendian and unaligned-capable */
87 *p = v;
88 #else
89 uint8_t *pp = (uint8_t *) p;
90 pp[0] = (v & 0xff);
91 pp[1] = ((v >> 8) & 0xff);
92 #endif
95 static inline void set_32(uint32_t *p, uint32_t v)
97 #if X86_MEM
98 /* Littleendian and unaligned-capable */
99 *p = v;
100 #else
101 uint8_t *pp = (uint8_t *) p;
102 pp[0] = (v & 0xff);
103 pp[1] = ((v >> 8) & 0xff);
104 pp[2] = ((v >> 16) & 0xff);
105 pp[3] = ((v >> 24) & 0xff);
106 #endif
109 static inline void set_64(uint64_t *p, uint64_t v)
111 #if X86_MEM
112 /* Littleendian and unaligned-capable */
113 *p = v;
114 #else
115 uint32_t *pp = (uint32_t *) p;
116 set_32(pp[0], v);
117 set_32(pp[1], v >> 32);
118 #endif
122 * Special handling for the MS-DOS derivative: syslinux_ldlinux
123 * is a "far" object...
125 #ifdef __MSDOS__
127 static inline __attribute__ ((const))
128 uint16_t ds(void)
130 uint16_t v;
131 asm("movw %%ds,%0":"=rm"(v));
132 return v;
135 static inline void *set_fs(const void *p)
137 uint16_t seg;
139 seg = ds() + ((size_t) p >> 4);
140 asm volatile ("movw %0,%%fs"::"rm" (seg));
141 return (void *)((size_t) p & 0xf);
144 uint8_t get_8_sl(const uint8_t * p);
145 uint16_t get_16_sl(const uint16_t * p);
146 uint32_t get_32_sl(const uint32_t * p);
147 uint64_t get_64_sl(const uint64_t * p);
148 void set_8_sl(uint8_t * p, uint8_t v);
149 void set_16_sl(uint16_t * p, uint16_t v);
150 void set_32_sl(uint32_t * p, uint32_t v);
151 void set_64_sl(uint64_t * p, uint64_t v);
152 void memcpy_to_sl(void *dst, const void *src, size_t len);
153 void memcpy_from_sl(void *dst, const void *src, size_t len);
155 #else
157 /* Sane system ... */
158 #define get_8_sl(x) get_8(x)
159 #define get_16_sl(x) get_16(x)
160 #define get_32_sl(x) get_32(x)
161 #define get_64_sl(x) get_64(x)
162 #define set_8_sl(x,y) set_8(x,y)
163 #define set_16_sl(x,y) set_16(x,y)
164 #define set_32_sl(x,y) set_32(x,y)
165 #define set_64_sl(x,y) set_64(x,y)
166 #define memcpy_to_sl(d,s,l) memcpy(d,s,l)
167 #define memcpy_from_sl(d,s,l) memcpy(d,s,l)
169 #endif
171 #define LDLINUX_MAGIC 0x3eb202fe
172 #define BS_MAGIC_VER (0x1b << 9)
174 /* Patch area for disk-based installers */
175 struct patch_area {
176 uint32_t magic; /* LDLINUX_MAGIC */
177 uint32_t instance; /* Per-version value */
178 uint16_t data_sectors;
179 uint16_t adv_sectors;
180 uint32_t dwords;
181 uint32_t checksum;
182 uint16_t maxtransfer;
183 uint16_t epaoffset; /* Pointer to the extended patch area */
186 struct ext_patch_area {
187 uint16_t advptroffset; /* ADV pointers */
188 uint16_t diroffset; /* Current directory field */
189 uint16_t dirlen; /* Length of current directory field */
190 uint16_t subvoloffset; /* Subvolume field */
191 uint16_t subvollen; /* Length of subvolume field */
192 uint16_t secptroffset; /* Sector extent pointers */
193 uint16_t secptrcnt; /* Number of sector extent pointers */
195 uint16_t sect1ptr0; /* Boot sector offset of sector 1 ptr LSW */
196 uint16_t sect1ptr1; /* Boot sector offset of sector 1 ptr MSW */
197 uint16_t raidpatch; /* Boot sector RAID mode patch pointer */
200 /* Sector extent */
201 struct syslinux_extent {
202 uint64_t lba;
203 uint16_t len;
204 } PACKED;
206 /* FAT bootsector format, also used by other disk-based derivatives */
207 struct fat_boot_sector {
208 uint8_t bsJump[3];
209 char bsOemName[8];
210 uint16_t bsBytesPerSec;
211 uint8_t bsSecPerClust;
212 uint16_t bsResSectors;
213 uint8_t bsFATs;
214 uint16_t bsRootDirEnts;
215 uint16_t bsSectors;
216 uint8_t bsMedia;
217 uint16_t bsFATsecs;
218 uint16_t bsSecPerTrack;
219 uint16_t bsHeads;
220 uint32_t bsHiddenSecs;
221 uint32_t bsHugeSectors;
223 union {
224 struct {
225 uint8_t DriveNumber;
226 uint8_t Reserved1;
227 uint8_t BootSignature;
228 uint32_t VolumeID;
229 char VolumeLabel[11];
230 char FileSysType[8];
231 uint8_t Code[442];
232 } PACKED bs16;
233 struct {
234 uint32_t FATSz32;
235 uint16_t ExtFlags;
236 uint16_t FSVer;
237 uint32_t RootClus;
238 uint16_t FSInfo;
239 uint16_t BkBootSec;
240 uint8_t Reserved0[12];
241 uint8_t DriveNumber;
242 uint8_t Reserved1;
243 uint8_t BootSignature;
244 uint32_t VolumeID;
245 char VolumeLabel[11];
246 char FileSysType[8];
247 uint8_t Code[414];
248 } PACKED bs32;
249 } PACKED;
251 uint32_t bsMagic;
252 uint16_t bsForwardPtr;
253 uint16_t bsSignature;
254 } PACKED;
256 /* NTFS bootsector format */
257 struct ntfs_boot_sector {
258 uint8_t bsJump[3];
259 char bsOemName[8];
260 uint16_t bsBytesPerSec;
261 uint8_t bsSecPerClust;
262 uint16_t bsResSectors;
263 uint8_t bsZeroed_0[3];
264 uint16_t bsZeroed_1;
265 uint8_t bsMedia;
266 uint16_t bsZeroed_2;
267 uint16_t bsUnused_0;
268 uint16_t bsUnused_1;
269 uint32_t bsUnused_2;
270 uint32_t bsZeroed_3;
271 uint32_t bsUnused_3;
272 uint64_t bsTotalSectors;
273 uint64_t bsMFTLogicalClustNr;
274 uint64_t bsMFTMirrLogicalClustNr;
275 uint8_t bsClustPerMFTrecord;
276 uint8_t bsUnused_4[3];
277 uint8_t bsClustPerIdxBuf;
278 uint8_t bsUnused_5[3];
279 uint64_t bsVolSerialNr;
280 uint32_t bsUnused_6;
282 uint8_t Code[420];
284 uint32_t bsMagic;
285 uint16_t bsForwardPtr;
286 uint16_t bsSignature;
287 } PACKED;
289 #define FAT_bsHead bsJump
290 #define FAT_bsHeadLen offsetof(struct fat_boot_sector, bsBytesPerSec)
291 #define FAT_bsCode bs32.Code /* The common safe choice */
292 #define FAT_bsCodeLen (offsetof(struct fat_boot_sector, bsSignature) - \
293 offsetof(struct fat_boot_sector, FAT_bsCode))
295 #define NTFS_bsHead bsJump
296 #define NTFS_bsHeadLen offsetof(struct ntfs_boot_sector, bsOemName)
297 #define NTFS_bsCode Code
298 #define NTFS_bsCodeLen (offsetof(struct ntfs_boot_sector, bsSignature) - \
299 offsetof(struct ntfs_boot_sector, NTFS_bsCode))
301 /* Check if there are specific zero fields in an NTFS boot sector */
302 static inline int ntfs_check_zero_fields(const struct ntfs_boot_sector *sb)
304 return !sb->bsResSectors && (!sb->bsZeroed_0[0] && !sb->bsZeroed_0[1] &&
305 !sb->bsZeroed_0[2]) && !sb->bsZeroed_1 && !sb->bsZeroed_2 &&
306 !sb->bsZeroed_3;
309 static inline int ntfs_check_sb_fields(const struct ntfs_boot_sector *sb)
311 return ntfs_check_zero_fields(sb) &&
312 (!memcmp(sb->bsOemName, "NTFS ", 8) ||
313 !memcmp(sb->bsOemName, "MSWIN4.0", 8) ||
314 !memcmp(sb->bsOemName, "MSWIN4.1", 8));
317 static inline int fat_check_sb_fields(const struct fat_boot_sector *sb)
319 return sb->bsResSectors && sb->bsFATs &&
320 (!memcmp(sb->bs16.FileSysType, "FAT12 ", 8) ||
321 !memcmp(sb->bs16.FileSysType, "FAT16 ", 8) ||
322 !memcmp(sb->bs16.FileSysType, "FAT ", 8) ||
323 !memcmp(sb->bs32.FileSysType, "FAT32 ", 8));
326 #endif /* SYSLXINT_H */