dmi: check both the AC and ID flags at the same time
[syslinux.git] / libinstaller / syslxint.h
blob9ff63f2e82b956f552dcaf1e6c95764280d9fde8
1 /* ----------------------------------------------------------------------- *
3 * Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
4 * Copyright 2009-2014 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__) || defined(_M_IX86) || defined(_M_X64)
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 PACKME
30 # define PACKED __attribute__((packed,gcc_struct))
31 # else
32 # define PACKME
33 # define PACKED __attribute__((packed))
34 # endif
35 #elif defined(_MSC_VER)
36 # define PACKME __pragma(pack(push, 1))
37 # define PACKED __pragma(pack(pop))
38 #else
39 # error "Need to define PACKED for this compiler"
40 #endif
43 * Access functions for littleendian numbers, possibly misaligned.
45 static inline uint8_t get_8(const uint8_t * p)
47 return *p;
50 static inline uint16_t get_16(const uint16_t * p)
52 #if X86_MEM
53 /* Littleendian and unaligned-capable */
54 return *p;
55 #else
56 const uint8_t *pp = (const uint8_t *)p;
57 return pp[0] + ((uint16_t)pp[1] << 8);
58 #endif
61 static inline uint32_t get_32(const uint32_t * p)
63 #if X86_MEM
64 /* Littleendian and unaligned-capable */
65 return *p;
66 #else
67 const uint16_t *pp = (const uint16_t *)p;
68 return get_16(&pp[0]) + ((uint32_t)get_16(&pp[1]) << 16);
69 #endif
72 static inline uint64_t get_64(const uint64_t * p)
74 #if X86_MEM
75 /* Littleendian and unaligned-capable */
76 return *p;
77 #else
78 const uint32_t *pp = (const uint32_t *)p;
79 return get_32(&pp[0]) + ((uint64_t)get_32(&pp[1]) << 32);
80 #endif
83 static inline void set_8(uint8_t *p, uint8_t v)
85 *p = v;
88 static inline void set_16(uint16_t *p, uint16_t v)
90 #if X86_MEM
91 /* Littleendian and unaligned-capable */
92 *p = v;
93 #else
94 uint8_t *pp = (uint8_t *) p;
95 pp[0] = v;
96 pp[1] = v >> 8;
97 #endif
100 static inline void set_32(uint32_t *p, uint32_t v)
102 #if X86_MEM
103 /* Littleendian and unaligned-capable */
104 *p = v;
105 #else
106 uint16_t *pp = (uint16_t *) p;
107 set_16(&pp[0], v);
108 set_16(&pp[1], v >> 16);
109 #endif
112 static inline void set_64(uint64_t *p, uint64_t v)
114 #if X86_MEM
115 /* Littleendian and unaligned-capable */
116 *p = v;
117 #else
118 uint32_t *pp = (uint32_t *) p;
119 set_32(&pp[0], v);
120 set_32(&pp[1], v >> 32);
121 #endif
125 * Special handling for the MS-DOS derivative: syslinux_ldlinux
126 * is a "far" object...
128 #ifdef __MSDOS__
130 uint8_t get_8_sl(const uint8_t _slimg * p);
131 uint16_t get_16_sl(const uint16_t _slimg * p);
132 uint32_t get_32_sl(const uint32_t _slimg * p);
133 uint64_t get_64_sl(const uint64_t _slimg * p);
134 void set_8_sl(uint8_t _slimg * p, uint8_t v);
135 void set_16_sl(uint16_t _slimg * p, uint16_t v);
136 void set_32_sl(uint32_t _slimg * p, uint32_t v);
137 void set_64_sl(uint64_t _slimg * p, uint64_t v);
138 void memcpy_to_sl(void _slimg *dst, const void *src, size_t len);
139 void memcpy_from_sl(void *dst, const void _slimg *src, size_t len);
140 void memset_sl(void _slimg *dst, int c, size_t len);
142 #else
144 /* Sane system ... */
145 static inline uint8_t get_8_sl(const uint8_t _slimg * p)
147 return get_8((const uint8_t _force *)p);
149 static inline uint16_t get_16_sl(const uint16_t _slimg * p)
151 return get_16((const uint16_t _force *)p);
153 static inline uint32_t get_32_sl(const uint32_t _slimg * p)
155 return get_32((const uint32_t _force *)p);
157 static inline uint64_t get_64_sl(const uint64_t _slimg * p)
159 return get_64((const uint64_t _force *)p);
161 static inline void set_8_sl(uint8_t _slimg * p, uint8_t v)
163 set_8((uint8_t _force *)p, v);
165 static inline void set_16_sl(uint16_t _slimg * p, uint16_t v)
167 set_16((uint16_t _force *)p, v);
169 static inline void set_32_sl(uint32_t _slimg * p, uint32_t v)
171 set_32((uint32_t _force *)p, v);
173 static inline void set_64_sl(uint64_t _slimg * p, uint64_t v)
175 set_64((uint64_t _force *)p, v);
177 static inline void memcpy_to_sl(void _slimg *dst, const void *src, size_t len)
179 memcpy((void _force *)dst, src, len);
181 static inline void memcpy_from_sl(void *dst, const void _slimg *src, size_t len)
183 memcpy(dst, (const void _force *)src, len);
185 static inline void memset_sl(void _slimg *dst, int c, size_t len)
187 memset((void _force *)dst, c, len);
190 #endif
192 #define LDLINUX_MAGIC 0x3eb202fe
193 #define BS_MAGIC_VER (0x1b << 9)
195 /* Patch area for disk-based installers */
196 struct patch_area {
197 uint32_t magic; /* LDLINUX_MAGIC */
198 uint32_t instance; /* Per-version value */
199 uint16_t data_sectors;
200 uint16_t adv_sectors;
201 uint32_t dwords;
202 uint32_t checksum;
203 uint16_t maxtransfer;
204 uint16_t epaoffset; /* Pointer to the extended patch area */
207 struct ext_patch_area {
208 uint16_t advptroffset; /* ADV pointers */
209 uint16_t diroffset; /* Current directory field */
210 uint16_t dirlen; /* Length of current directory field */
211 uint16_t subvoloffset; /* Subvolume field */
212 uint16_t subvollen; /* Length of subvolume field */
213 uint16_t secptroffset; /* Sector extent pointers */
214 uint16_t secptrcnt; /* Number of sector extent pointers */
216 uint16_t sect1ptr0; /* Boot sector offset of sector 1 ptr LSW */
217 uint16_t sect1ptr1; /* Boot sector offset of sector 1 ptr MSW */
218 uint16_t raidpatch; /* Boot sector RAID mode patch pointer */
221 /* Sector extent */
222 PACKME
223 struct syslinux_extent {
224 uint64_t lba;
225 uint16_t len;
226 } PACKED;
228 /* FAT bootsector format, also used by other disk-based derivatives */
229 PACKME
230 struct fat_boot_sector {
231 uint8_t bsJump[3];
232 char bsOemName[8];
233 uint16_t bsBytesPerSec;
234 uint8_t bsSecPerClust;
235 uint16_t bsResSectors;
236 uint8_t bsFATs;
237 uint16_t bsRootDirEnts;
238 uint16_t bsSectors;
239 uint8_t bsMedia;
240 uint16_t bsFATsecs;
241 uint16_t bsSecPerTrack;
242 uint16_t bsHeads;
243 uint32_t bsHiddenSecs;
244 uint32_t bsHugeSectors;
246 PACKME
247 union {
248 PACKME
249 struct {
250 uint8_t DriveNumber;
251 uint8_t Reserved1;
252 uint8_t BootSignature;
253 uint32_t VolumeID;
254 char VolumeLabel[11];
255 char FileSysType[8];
256 uint8_t Code[442];
257 } PACKED bs16;
258 PACKME
259 struct {
260 uint32_t FATSz32;
261 uint16_t ExtFlags;
262 uint16_t FSVer;
263 uint32_t RootClus;
264 uint16_t FSInfo;
265 uint16_t BkBootSec;
266 uint8_t Reserved0[12];
267 uint8_t DriveNumber;
268 uint8_t Reserved1;
269 uint8_t BootSignature;
270 uint32_t VolumeID;
271 char VolumeLabel[11];
272 char FileSysType[8];
273 uint8_t Code[414];
274 } PACKED bs32;
275 } PACKED;
277 uint32_t bsMagic;
278 uint16_t bsForwardPtr;
279 uint16_t bsSignature;
280 } PACKED;
282 /* NTFS bootsector format */
283 PACKME
284 struct ntfs_boot_sector {
285 uint8_t bsJump[3];
286 char bsOemName[8];
287 uint16_t bsBytesPerSec;
288 uint8_t bsSecPerClust;
289 uint16_t bsResSectors;
290 uint8_t bsZeroed_0[3];
291 uint16_t bsZeroed_1;
292 uint8_t bsMedia;
293 uint16_t bsZeroed_2;
294 uint16_t bsUnused_0;
295 uint16_t bsUnused_1;
296 uint32_t bsUnused_2;
297 uint32_t bsZeroed_3;
298 uint32_t bsUnused_3;
299 uint64_t bsTotalSectors;
300 uint64_t bsMFTLogicalClustNr;
301 uint64_t bsMFTMirrLogicalClustNr;
302 uint8_t bsClustPerMFTrecord;
303 uint8_t bsUnused_4[3];
304 uint8_t bsClustPerIdxBuf;
305 uint8_t bsUnused_5[3];
306 uint64_t bsVolSerialNr;
307 uint32_t bsUnused_6;
309 uint8_t Code[420];
311 uint32_t bsMagic;
312 uint16_t bsForwardPtr;
313 uint16_t bsSignature;
314 } PACKED;
316 #define FAT_bsHead bsJump
317 #define FAT_bsHeadLen offsetof(struct fat_boot_sector, bsBytesPerSec)
318 #define FAT_bsCode bs32.Code /* The common safe choice */
319 #define FAT_bsCodeLen (offsetof(struct fat_boot_sector, bsSignature) - \
320 offsetof(struct fat_boot_sector, FAT_bsCode))
322 #define NTFS_bsHead bsJump
323 #define NTFS_bsHeadLen offsetof(struct ntfs_boot_sector, bsOemName)
324 #define NTFS_bsCode Code
325 #define NTFS_bsCodeLen (offsetof(struct ntfs_boot_sector, bsSignature) - \
326 offsetof(struct ntfs_boot_sector, NTFS_bsCode))
328 /* Check if there are specific zero fields in an NTFS boot sector */
329 static inline int ntfs_check_zero_fields(const struct ntfs_boot_sector *sb)
331 return !sb->bsResSectors && (!sb->bsZeroed_0[0] && !sb->bsZeroed_0[1] &&
332 !sb->bsZeroed_0[2]) && !sb->bsZeroed_1 && !sb->bsZeroed_2 &&
333 !sb->bsZeroed_3;
336 static inline int ntfs_check_sb_fields(const struct ntfs_boot_sector *sb)
338 return ntfs_check_zero_fields(sb) &&
339 (!memcmp(sb->bsOemName, "NTFS ", 8) ||
340 !memcmp(sb->bsOemName, "MSWIN4.0", 8) ||
341 !memcmp(sb->bsOemName, "MSWIN4.1", 8));
344 static inline int fat_check_sb_fields(const struct fat_boot_sector *sb)
346 return sb->bsResSectors && sb->bsFATs &&
347 (!memcmp(sb->bs16.FileSysType, "FAT12 ", 8) ||
348 !memcmp(sb->bs16.FileSysType, "FAT16 ", 8) ||
349 !memcmp(sb->bs16.FileSysType, "FAT ", 8) ||
350 !memcmp(sb->bs32.FileSysType, "FAT32 ", 8));
353 #endif /* SYSLXINT_H */