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 static inline __attribute__ ((const))
131 asm("movw %%ds,%0":"=rm"(v
));
135 static inline void *set_fs(const void *p
)
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
);
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)
171 #define LDLINUX_MAGIC 0x3eb202fe
172 #define BS_MAGIC_VER (0x1b << 9)
174 /* Patch area for disk-based installers */
176 uint32_t magic
; /* LDLINUX_MAGIC */
177 uint32_t instance
; /* Per-version value */
178 uint16_t data_sectors
;
179 uint16_t adv_sectors
;
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 */
201 struct syslinux_extent
{
206 /* FAT bootsector format, also used by other disk-based derivatives */
207 struct fat_boot_sector
{
210 uint16_t bsBytesPerSec
;
211 uint8_t bsSecPerClust
;
212 uint16_t bsResSectors
;
214 uint16_t bsRootDirEnts
;
218 uint16_t bsSecPerTrack
;
220 uint32_t bsHiddenSecs
;
221 uint32_t bsHugeSectors
;
227 uint8_t BootSignature
;
229 char VolumeLabel
[11];
240 uint8_t Reserved0
[12];
243 uint8_t BootSignature
;
245 char VolumeLabel
[11];
252 uint16_t bsForwardPtr
;
253 uint16_t bsSignature
;
256 /* NTFS bootsector format */
257 struct ntfs_boot_sector
{
260 uint16_t bsBytesPerSec
;
261 uint8_t bsSecPerClust
;
262 uint16_t bsResSectors
;
263 uint8_t bsZeroed_0
[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
;
285 uint16_t bsForwardPtr
;
286 uint16_t bsSignature
;
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
&&
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 */