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 * ----------------------------------------------------------------------- */
20 #if defined(__386__) || defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64)
21 # define X86_MEM 1 /* Littleendian and unaligned safe */
28 /* gcc 4.7 miscompiles packed structures in MS-bitfield mode */
30 # define PACKED __attribute__((packed,gcc_struct))
33 # define PACKED __attribute__((packed))
35 #elif defined(_MSC_VER)
36 # define PACKME __pragma(pack(push, 1))
37 # define PACKED __pragma(pack(pop))
39 # error "Need to define PACKED for this compiler"
43 * Access functions for littleendian numbers, possibly misaligned.
45 static inline uint8_t get_8(const uint8_t * p
)
50 static inline uint16_t get_16(const uint16_t * p
)
53 /* Littleendian and unaligned-capable */
56 const uint8_t *pp
= (const uint8_t *)p
;
57 return pp
[0] + ((uint16_t)pp
[1] << 8);
61 static inline uint32_t get_32(const uint32_t * p
)
64 /* Littleendian and unaligned-capable */
67 const uint16_t *pp
= (const uint16_t *)p
;
68 return get_16(&pp
[0]) + ((uint32_t)get_16(&pp
[1]) << 16);
72 static inline uint64_t get_64(const uint64_t * p
)
75 /* Littleendian and unaligned-capable */
78 const uint32_t *pp
= (const uint32_t *)p
;
79 return get_32(&pp
[0]) + ((uint64_t)get_32(&pp
[1]) << 32);
83 static inline void set_8(uint8_t *p
, uint8_t v
)
88 static inline void set_16(uint16_t *p
, uint16_t v
)
91 /* Littleendian and unaligned-capable */
94 uint8_t *pp
= (uint8_t *) p
;
100 static inline void set_32(uint32_t *p
, uint32_t v
)
103 /* Littleendian and unaligned-capable */
106 uint16_t *pp
= (uint16_t *) p
;
108 set_16(&pp
[1], v
>> 16);
112 static inline void set_64(uint64_t *p
, uint64_t v
)
115 /* Littleendian and unaligned-capable */
118 uint32_t *pp
= (uint32_t *) p
;
120 set_32(&pp
[1], v
>> 32);
125 * Special handling for the MS-DOS derivative: syslinux_ldlinux
126 * is a "far" object...
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
);
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
);
192 #define LDLINUX_MAGIC 0x3eb202fe
193 #define BS_MAGIC_VER (0x1b << 9)
195 /* Patch area for disk-based installers */
197 uint32_t magic
; /* LDLINUX_MAGIC */
198 uint32_t instance
; /* Per-version value */
199 uint16_t data_sectors
;
200 uint16_t adv_sectors
;
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 */
223 struct syslinux_extent
{
228 /* FAT bootsector format, also used by other disk-based derivatives */
230 struct fat_boot_sector
{
233 uint16_t bsBytesPerSec
;
234 uint8_t bsSecPerClust
;
235 uint16_t bsResSectors
;
237 uint16_t bsRootDirEnts
;
241 uint16_t bsSecPerTrack
;
243 uint32_t bsHiddenSecs
;
244 uint32_t bsHugeSectors
;
252 uint8_t BootSignature
;
254 char VolumeLabel
[11];
266 uint8_t Reserved0
[12];
269 uint8_t BootSignature
;
271 char VolumeLabel
[11];
278 uint16_t bsForwardPtr
;
279 uint16_t bsSignature
;
282 /* NTFS bootsector format */
284 struct ntfs_boot_sector
{
287 uint16_t bsBytesPerSec
;
288 uint8_t bsSecPerClust
;
289 uint16_t bsResSectors
;
290 uint8_t bsZeroed_0
[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
;
312 uint16_t bsForwardPtr
;
313 uint16_t bsSignature
;
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
&&
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 */