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 * ----------------------------------------------------------------------- */
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]);
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]);
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
;
91 pp
[1] = ((v
>> 8) & 0xff);
95 static inline void set_32(uint32_t *p
, uint32_t v
)
98 /* Littleendian and unaligned-capable */
101 uint8_t *pp
= (uint8_t *) p
;
103 pp
[1] = ((v
>> 8) & 0xff);
104 pp
[2] = ((v
>> 16) & 0xff);
105 pp
[3] = ((v
>> 24) & 0xff);
109 static inline void set_64(uint64_t *p
, uint64_t v
)
112 /* Littleendian and unaligned-capable */
115 uint32_t *pp
= (uint32_t *) p
;
117 set_32(pp
[1], v
>> 32);
122 * Special handling for the MS-DOS derivative: syslinux_ldlinux
123 * is a "far" object...
127 uint8_t get_8_sl(const uint8_t * p
);
128 uint16_t get_16_sl(const uint16_t * p
);
129 uint32_t get_32_sl(const uint32_t * p
);
130 uint64_t get_64_sl(const uint64_t * p
);
131 void set_8_sl(uint8_t * p
, uint8_t v
);
132 void set_16_sl(uint16_t * p
, uint16_t v
);
133 void set_32_sl(uint32_t * p
, uint32_t v
);
134 void set_64_sl(uint64_t * p
, uint64_t v
);
135 void memcpy_to_sl(void *dst
, const void *src
, size_t len
);
136 void memcpy_from_sl(void *dst
, const void *src
, size_t len
);
140 /* Sane system ... */
141 #define get_8_sl(x) get_8(x)
142 #define get_16_sl(x) get_16(x)
143 #define get_32_sl(x) get_32(x)
144 #define get_64_sl(x) get_64(x)
145 #define set_8_sl(x,y) set_8(x,y)
146 #define set_16_sl(x,y) set_16(x,y)
147 #define set_32_sl(x,y) set_32(x,y)
148 #define set_64_sl(x,y) set_64(x,y)
149 #define memcpy_to_sl(d,s,l) memcpy(d,s,l)
150 #define memcpy_from_sl(d,s,l) memcpy(d,s,l)
154 #define LDLINUX_MAGIC 0x3eb202fe
155 #define BS_MAGIC_VER (0x1b << 9)
157 /* Patch area for disk-based installers */
159 uint32_t magic
; /* LDLINUX_MAGIC */
160 uint32_t instance
; /* Per-version value */
161 uint16_t data_sectors
;
162 uint16_t adv_sectors
;
165 uint16_t maxtransfer
;
166 uint16_t epaoffset
; /* Pointer to the extended patch area */
169 struct ext_patch_area
{
170 uint16_t advptroffset
; /* ADV pointers */
171 uint16_t diroffset
; /* Current directory field */
172 uint16_t dirlen
; /* Length of current directory field */
173 uint16_t subvoloffset
; /* Subvolume field */
174 uint16_t subvollen
; /* Length of subvolume field */
175 uint16_t secptroffset
; /* Sector extent pointers */
176 uint16_t secptrcnt
; /* Number of sector extent pointers */
178 uint16_t sect1ptr0
; /* Boot sector offset of sector 1 ptr LSW */
179 uint16_t sect1ptr1
; /* Boot sector offset of sector 1 ptr MSW */
180 uint16_t raidpatch
; /* Boot sector RAID mode patch pointer */
184 struct syslinux_extent
{
189 /* FAT bootsector format, also used by other disk-based derivatives */
190 struct fat_boot_sector
{
193 uint16_t bsBytesPerSec
;
194 uint8_t bsSecPerClust
;
195 uint16_t bsResSectors
;
197 uint16_t bsRootDirEnts
;
201 uint16_t bsSecPerTrack
;
203 uint32_t bsHiddenSecs
;
204 uint32_t bsHugeSectors
;
210 uint8_t BootSignature
;
212 char VolumeLabel
[11];
223 uint8_t Reserved0
[12];
226 uint8_t BootSignature
;
228 char VolumeLabel
[11];
235 uint16_t bsForwardPtr
;
236 uint16_t bsSignature
;
239 /* NTFS bootsector format */
240 struct ntfs_boot_sector
{
243 uint16_t bsBytesPerSec
;
244 uint8_t bsSecPerClust
;
245 uint16_t bsResSectors
;
246 uint8_t bsZeroed_0
[3];
255 uint64_t bsTotalSectors
;
256 uint64_t bsMFTLogicalClustNr
;
257 uint64_t bsMFTMirrLogicalClustNr
;
258 uint8_t bsClustPerMFTrecord
;
259 uint8_t bsUnused_4
[3];
260 uint8_t bsClustPerIdxBuf
;
261 uint8_t bsUnused_5
[3];
262 uint64_t bsVolSerialNr
;
268 uint16_t bsForwardPtr
;
269 uint16_t bsSignature
;
272 #define FAT_bsHead bsJump
273 #define FAT_bsHeadLen offsetof(struct fat_boot_sector, bsBytesPerSec)
274 #define FAT_bsCode bs32.Code /* The common safe choice */
275 #define FAT_bsCodeLen (offsetof(struct fat_boot_sector, bsSignature) - \
276 offsetof(struct fat_boot_sector, FAT_bsCode))
278 #define NTFS_bsHead bsJump
279 #define NTFS_bsHeadLen offsetof(struct ntfs_boot_sector, bsOemName)
280 #define NTFS_bsCode Code
281 #define NTFS_bsCodeLen (offsetof(struct ntfs_boot_sector, bsSignature) - \
282 offsetof(struct ntfs_boot_sector, NTFS_bsCode))
284 /* Check if there are specific zero fields in an NTFS boot sector */
285 static inline int ntfs_check_zero_fields(const struct ntfs_boot_sector
*sb
)
287 return !sb
->bsResSectors
&& (!sb
->bsZeroed_0
[0] && !sb
->bsZeroed_0
[1] &&
288 !sb
->bsZeroed_0
[2]) && !sb
->bsZeroed_1
&& !sb
->bsZeroed_2
&&
292 static inline int ntfs_check_sb_fields(const struct ntfs_boot_sector
*sb
)
294 return ntfs_check_zero_fields(sb
) &&
295 (!memcmp(sb
->bsOemName
, "NTFS ", 8) ||
296 !memcmp(sb
->bsOemName
, "MSWIN4.0", 8) ||
297 !memcmp(sb
->bsOemName
, "MSWIN4.1", 8));
300 static inline int fat_check_sb_fields(const struct fat_boot_sector
*sb
)
302 return sb
->bsResSectors
&& sb
->bsFATs
&&
303 (!memcmp(sb
->bs16
.FileSysType
, "FAT12 ", 8) ||
304 !memcmp(sb
->bs16
.FileSysType
, "FAT16 ", 8) ||
305 !memcmp(sb
->bs16
.FileSysType
, "FAT ", 8) ||
306 !memcmp(sb
->bs32
.FileSysType
, "FAT32 ", 8));
309 #endif /* SYSLXINT_H */