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__)
21 # define X86_MEM 1 /* Littleendian and unaligned safe */
28 /* gcc 4.7 miscompiles packed structures in MS-bitfield mode */
29 # define PACKED __attribute__((packed,gcc_struct))
31 # define PACKED __attribute__((packed))
34 # error "Need to define PACKED for this compiler"
38 * Access functions for littleendian numbers, possibly misaligned.
40 static inline uint8_t get_8(const uint8_t * p
)
45 static inline uint16_t get_16(const uint16_t * p
)
48 /* Littleendian and unaligned-capable */
51 const uint8_t *pp
= (const uint8_t *)p
;
52 return pp
[0] + ((uint16_t)pp
[1] << 8);
56 static inline uint32_t get_32(const uint32_t * p
)
59 /* Littleendian and unaligned-capable */
62 const uint16_t *pp
= (const uint16_t *)p
;
63 return get_16(&pp
[0]) + ((uint32_t)get_16(&pp
[1]) << 16);
67 static inline uint64_t get_64(const uint64_t * p
)
70 /* Littleendian and unaligned-capable */
73 const uint32_t *pp
= (const uint32_t *)p
;
74 return get_32(&pp
[0]) + ((uint64_t)get_32(&pp
[1]) << 32);
78 static inline void set_8(uint8_t *p
, uint8_t v
)
83 static inline void set_16(uint16_t *p
, uint16_t v
)
86 /* Littleendian and unaligned-capable */
89 uint8_t *pp
= (uint8_t *) p
;
95 static inline void set_32(uint32_t *p
, uint32_t v
)
98 /* Littleendian and unaligned-capable */
101 uint16_t *pp
= (uint16_t *) p
;
103 set_16(&pp
[1], v
>> 16);
107 static inline void set_64(uint64_t *p
, uint64_t v
)
110 /* Littleendian and unaligned-capable */
113 uint32_t *pp
= (uint32_t *) p
;
115 set_32(&pp
[1], v
>> 32);
120 * Special handling for the MS-DOS derivative: syslinux_ldlinux
121 * is a "far" object...
125 uint8_t get_8_sl(const uint8_t _slimg
* p
);
126 uint16_t get_16_sl(const uint16_t _slimg
* p
);
127 uint32_t get_32_sl(const uint32_t _slimg
* p
);
128 uint64_t get_64_sl(const uint64_t _slimg
* p
);
129 void set_8_sl(uint8_t _slimg
* p
, uint8_t v
);
130 void set_16_sl(uint16_t _slimg
* p
, uint16_t v
);
131 void set_32_sl(uint32_t _slimg
* p
, uint32_t v
);
132 void set_64_sl(uint64_t _slimg
* p
, uint64_t v
);
133 void memcpy_to_sl(void _slimg
*dst
, const void *src
, size_t len
);
134 void memcpy_from_sl(void *dst
, const void _slimg
*src
, size_t len
);
135 void memset_sl(void _slimg
*dst
, int c
, size_t len
);
139 /* Sane system ... */
140 static inline uint8_t get_8_sl(const uint8_t _slimg
* p
)
142 return get_8((const uint8_t _force
*)p
);
144 static inline uint16_t get_16_sl(const uint16_t _slimg
* p
)
146 return get_16((const uint16_t _force
*)p
);
148 static inline uint32_t get_32_sl(const uint32_t _slimg
* p
)
150 return get_32((const uint32_t _force
*)p
);
152 static inline uint64_t get_64_sl(const uint64_t _slimg
* p
)
154 return get_64((const uint64_t _force
*)p
);
156 static inline void set_8_sl(uint8_t _slimg
* p
, uint8_t v
)
158 set_8((uint8_t _force
*)p
, v
);
160 static inline void set_16_sl(uint16_t _slimg
* p
, uint16_t v
)
162 set_16((uint16_t _force
*)p
, v
);
164 static inline void set_32_sl(uint32_t _slimg
* p
, uint32_t v
)
166 set_32((uint32_t _force
*)p
, v
);
168 static inline void set_64_sl(uint64_t _slimg
* p
, uint64_t v
)
170 set_64((uint64_t _force
*)p
, v
);
172 static inline void memcpy_to_sl(void _slimg
*dst
, const void *src
, size_t len
)
174 memcpy((void _force
*)dst
, src
, len
);
176 static inline void memcpy_from_sl(void *dst
, const void _slimg
*src
, size_t len
)
178 memcpy(dst
, (const void _force
*)src
, len
);
180 static inline void memset_sl(void _slimg
*dst
, int c
, size_t len
)
182 memset((void _force
*)dst
, c
, len
);
187 #define LDLINUX_MAGIC 0x3eb202fe
188 #define BS_MAGIC_VER (0x1b << 9)
190 /* Patch area for disk-based installers */
192 uint32_t magic
; /* LDLINUX_MAGIC */
193 uint32_t instance
; /* Per-version value */
194 uint16_t data_sectors
;
195 uint16_t adv_sectors
;
198 uint16_t maxtransfer
;
199 uint16_t epaoffset
; /* Pointer to the extended patch area */
202 struct ext_patch_area
{
203 uint16_t advptroffset
; /* ADV pointers */
204 uint16_t diroffset
; /* Current directory field */
205 uint16_t dirlen
; /* Length of current directory field */
206 uint16_t subvoloffset
; /* Subvolume field */
207 uint16_t subvollen
; /* Length of subvolume field */
208 uint16_t secptroffset
; /* Sector extent pointers */
209 uint16_t secptrcnt
; /* Number of sector extent pointers */
211 uint16_t sect1ptr0
; /* Boot sector offset of sector 1 ptr LSW */
212 uint16_t sect1ptr1
; /* Boot sector offset of sector 1 ptr MSW */
213 uint16_t raidpatch
; /* Boot sector RAID mode patch pointer */
217 struct syslinux_extent
{
222 /* FAT bootsector format, also used by other disk-based derivatives */
223 struct fat_boot_sector
{
226 uint16_t bsBytesPerSec
;
227 uint8_t bsSecPerClust
;
228 uint16_t bsResSectors
;
230 uint16_t bsRootDirEnts
;
234 uint16_t bsSecPerTrack
;
236 uint32_t bsHiddenSecs
;
237 uint32_t bsHugeSectors
;
243 uint8_t BootSignature
;
245 char VolumeLabel
[11];
256 uint8_t Reserved0
[12];
259 uint8_t BootSignature
;
261 char VolumeLabel
[11];
268 uint16_t bsForwardPtr
;
269 uint16_t bsSignature
;
272 /* NTFS bootsector format */
273 struct ntfs_boot_sector
{
276 uint16_t bsBytesPerSec
;
277 uint8_t bsSecPerClust
;
278 uint16_t bsResSectors
;
279 uint8_t bsZeroed_0
[3];
288 uint64_t bsTotalSectors
;
289 uint64_t bsMFTLogicalClustNr
;
290 uint64_t bsMFTMirrLogicalClustNr
;
291 uint8_t bsClustPerMFTrecord
;
292 uint8_t bsUnused_4
[3];
293 uint8_t bsClustPerIdxBuf
;
294 uint8_t bsUnused_5
[3];
295 uint64_t bsVolSerialNr
;
301 uint16_t bsForwardPtr
;
302 uint16_t bsSignature
;
305 #define FAT_bsHead bsJump
306 #define FAT_bsHeadLen offsetof(struct fat_boot_sector, bsBytesPerSec)
307 #define FAT_bsCode bs32.Code /* The common safe choice */
308 #define FAT_bsCodeLen (offsetof(struct fat_boot_sector, bsSignature) - \
309 offsetof(struct fat_boot_sector, FAT_bsCode))
311 #define NTFS_bsHead bsJump
312 #define NTFS_bsHeadLen offsetof(struct ntfs_boot_sector, bsOemName)
313 #define NTFS_bsCode Code
314 #define NTFS_bsCodeLen (offsetof(struct ntfs_boot_sector, bsSignature) - \
315 offsetof(struct ntfs_boot_sector, NTFS_bsCode))
317 /* Check if there are specific zero fields in an NTFS boot sector */
318 static inline int ntfs_check_zero_fields(const struct ntfs_boot_sector
*sb
)
320 return !sb
->bsResSectors
&& (!sb
->bsZeroed_0
[0] && !sb
->bsZeroed_0
[1] &&
321 !sb
->bsZeroed_0
[2]) && !sb
->bsZeroed_1
&& !sb
->bsZeroed_2
&&
325 static inline int ntfs_check_sb_fields(const struct ntfs_boot_sector
*sb
)
327 return ntfs_check_zero_fields(sb
) &&
328 (!memcmp(sb
->bsOemName
, "NTFS ", 8) ||
329 !memcmp(sb
->bsOemName
, "MSWIN4.0", 8) ||
330 !memcmp(sb
->bsOemName
, "MSWIN4.1", 8));
333 static inline int fat_check_sb_fields(const struct fat_boot_sector
*sb
)
335 return sb
->bsResSectors
&& sb
->bsFATs
&&
336 (!memcmp(sb
->bs16
.FileSysType
, "FAT12 ", 8) ||
337 !memcmp(sb
->bs16
.FileSysType
, "FAT16 ", 8) ||
338 !memcmp(sb
->bs16
.FileSysType
, "FAT ", 8) ||
339 !memcmp(sb
->bs32
.FileSysType
, "FAT32 ", 8));
342 #endif /* SYSLXINT_H */