Switch to 64-bit sector pointers everywhere
[profile/ivi/syslinux.git] / libinstaller / syslxint.h
1 /* ----------------------------------------------------------------------- *
2  *
3  *   Copyright 2007-2008 H. Peter Anvin - All Rights Reserved
4  *
5  *   This program is free software; you can redistribute it and/or modify
6  *   it under the terms of the GNU General Public License as published by
7  *   the Free Software Foundation, Inc., 53 Temple Place Ste 330,
8  *   Boston MA 02111-1307, USA; either version 2 of the License, or
9  *   (at your option) any later version; incorporated herein by reference.
10  *
11  * ----------------------------------------------------------------------- */
12
13 #ifndef SYSLXINT_H
14 #define SYSLXINT_H
15
16 #include "syslinux.h"
17
18 #if defined(__386__) || defined(__i386__) || defined(__x86_64__)
19 # define X86_MEM 1              /* Littleendian and unaligned safe */
20 #else
21 # define X86_MEM 0
22 #endif
23
24 /*
25  * Access functions for littleendian numbers, possibly misaligned.
26  */
27 static inline uint8_t get_8(const uint8_t * p)
28 {
29     return *p;
30 }
31
32 static inline uint16_t get_16(const uint16_t * p)
33 {
34 #if X86_MEM
35     /* Littleendian and unaligned-capable */
36     return *p;
37 #else
38     const uint8_t *pp = (const uint8_t *)p;
39     return pp[0] + ((uint16_t)pp[1] << 8);
40 #endif
41 }
42
43 static inline uint32_t get_32(const uint32_t * p)
44 {
45 #if X86_MEM
46     /* Littleendian and unaligned-capable */
47     return *p;
48 #else
49     const uint16_t *pp = (const uint16_t *)p;
50     return get_16(pp[0]) + (uint32_t)get_16(pp[1]);
51 #endif
52 }
53
54 static inline uint64_t get_64(const uint64_t * p)
55 {
56 #if X86_MEM
57     /* Littleendian and unaligned-capable */
58     return *p;
59 #else
60     const uint32_t *pp = (const uint32_t *)p;
61     return get_32(pp[0]) + (uint64_t)get_32(pp[1]);
62 #endif
63 }
64
65 static inline void set_8(uint8_t *p, uint8_t v)
66 {
67     *p = v;
68 }
69
70 static inline void set_16(uint16_t *p, uint16_t v)
71 {
72 #if X86_MEM
73     /* Littleendian and unaligned-capable */
74     *p = v;
75 #else
76     uint8_t *pp = (uint8_t *) p;
77     pp[0] = (v & 0xff);
78     pp[1] = ((v >> 8) & 0xff);
79 #endif
80 }
81
82 static inline void set_32(uint32_t *p, uint32_t v)
83 {
84 #if X86_MEM
85     /* Littleendian and unaligned-capable */
86     *p = v;
87 #else
88     uint8_t *pp = (uint8_t *) p;
89     pp[0] = (v & 0xff);
90     pp[1] = ((v >> 8) & 0xff);
91     pp[2] = ((v >> 16) & 0xff);
92     pp[3] = ((v >> 24) & 0xff);
93 #endif
94 }
95
96 static inline void set_64(uint64_t *p, uint64_t v)
97 {
98 #if X86_MEM
99     /* Littleendian and unaligned-capable */
100     *p = v;
101 #else
102     uint32_t *pp = (uint32_t *) p;
103     set_32(pp[0], v);
104     set_32(pp[1], v >> 32);
105 #endif
106 }
107
108 #define LDLINUX_MAGIC   0x3eb202fe
109
110 /* Patch area for disk-based installers */
111 struct patch_area {
112     uint32_t magic;             /* LDLINUX_MAGIC */
113     uint32_t instance;          /* Per-version value */
114     uint16_t data_sectors;
115     uint16_t adv_sectors;
116     uint32_t dwords;
117     uint32_t checksum;
118     uint16_t maxtransfer;
119     uint16_t advptroffset;
120     uint16_t diroffset;
121     uint16_t dirlen;
122     uint16_t subvoloffset;
123     uint16_t subvollen;
124     uint16_t secptroffset;
125     uint16_t secptrcnt;
126 };
127
128 /* Sector extent */
129 struct syslinux_extent {
130     uint64_t lba;
131     uint16_t len;
132 } __attribute__((packed));
133
134 /* FAT bootsector format, also used by other disk-based derivatives */
135 struct boot_sector {
136     uint8_t bsJump[3];
137     char bsOemName[8];
138     uint16_t bsBytesPerSec;
139     uint8_t bsSecPerClust;
140     uint16_t bsResSectors;
141     uint8_t bsFATs;
142     uint16_t bsRootDirEnts;
143     uint16_t bsSectors;
144     uint8_t bsMedia;
145     uint16_t bsFATsecs;
146     uint16_t bsSecPerTrack;
147     uint16_t bsHeads;
148     uint32_t bsHiddenSecs;
149     uint32_t bsHugeSectors;
150
151     union {
152         struct {
153             uint8_t DriveNumber;
154             uint8_t Reserved1;
155             uint8_t BootSignature;
156             uint32_t VolumeID;
157             char VolumeLabel[11];
158             char FileSysType[8];
159             uint8_t Code[440];
160         } __attribute__ ((packed)) bs16;
161         struct {
162             uint32_t FATSz32;
163             uint16_t ExtFlags;
164             uint16_t FSVer;
165             uint32_t RootClus;
166             uint16_t FSInfo;
167             uint16_t BkBootSec;
168             uint8_t Reserved0[12];
169             uint8_t DriveNumber;
170             uint8_t Reserved1;
171             uint8_t BootSignature;
172             uint32_t VolumeID;
173             char VolumeLabel[11];
174             char FileSysType[8];
175             uint8_t Code[412];
176         } __attribute__ ((packed)) bs32;
177     } __attribute__ ((packed));
178
179     uint64_t NextSector;        /* Pointer to the first unused sector */
180     uint16_t bsSignature;
181 } __attribute__ ((packed));
182
183 #define bsHead      bsJump
184 #define bsHeadLen   offsetof(struct boot_sector, bsOemName)
185 #define bsCode      bs32.Code   /* The common safe choice */
186 #define bsCodeLen   (offsetof(struct boot_sector, bsSignature) - \
187                      offsetof(struct boot_sector, bsCode))
188
189 #endif /* SYSLXINT_H */