ms-sys  2.7.0
About: ms-sys writes Microsoft compatible boot records. Development version.
  Fossies Dox: ms-sys-2.7.0.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

br.c
Go to the documentation of this file.
1 
18 #include <stdio.h>
19 
20 #include "file.h"
21 #include "nls.h"
22 #include "br.h"
23 
24 static unsigned long ulBytesPerSector = 512;
25 
26 void set_bytes_per_sector(unsigned long ulValue)
27 {
28  ulBytesPerSector = ulValue;
29  if ((ulBytesPerSector < 512) || (ulBytesPerSector > 65536))
30  ulBytesPerSector = 512;
31 } /* set_bytes_per_sector */
32 
33 uint32_t read_windows_disk_signature(FILE *fp)
34 {
35  uint32_t tWDS;
36  if(!read_data(fp, 0x1b8, &tWDS, 4))
37  return 0;
38  return tWDS;
39 } /* read_windows_disk_signature */
40 
41 int write_windows_disk_signature(FILE *fp, uint32_t tWDS)
42 {
43  return write_data(fp, 0x1b8, &tWDS, 4);
44 } /* write_windows_disk_signature */
45 
46 uint16_t read_mbr_copy_protect_bytes(FILE *fp)
47 {
48  uint16_t tOut;
49  if(!read_data(fp, 0x1bc, &tOut, 2))
50  return 0xffff;
51  return tOut;
52 } /* read_mbr_copy_protect_bytes */
53 
55 {
56  uint16_t t = read_mbr_copy_protect_bytes(fp);
57  switch(t)
58  {
59  case 0:
60  return _("not copy protected");
61  case 0x5a5a:
62  return _("copy protected");
63  default:
64  return _("unknown value");
65  }
66 } /* read_mbr_copy_protect_bytes_explained */
67 
68 int is_br(FILE *fp)
69 {
70  /* A "file" is probably some kind of boot record if it contains the magic
71  chars 0x55, 0xAA at position 0x1FE */
72  unsigned char aucRef[] = {0x55, 0xAA};
73 
74  return contains_data(fp, 0x1FE, aucRef, sizeof(aucRef));
75 } /* is_br */
76 
77 int is_lilo_br(FILE *fp)
78 {
79  /* A "file" is probably a LILO boot record if it contains the magic
80  chars LILO at position 0x6 or 0x2 for floppies */
81  unsigned char aucRef[] = {'L','I','L','O'};
82 
83  return ( contains_data(fp, 0x6, aucRef, sizeof(aucRef)) ||
84  contains_data(fp, 0x2, aucRef, sizeof(aucRef)) );
85 } /* is_lilo_br */
86 
87 int is_dos_mbr(FILE *fp)
88 {
89  #include "mbr_dos.h"
90 
91  return
92  contains_data(fp, 0x0, mbr_dos_0x0, sizeof(mbr_dos_0x0)) &&
93  is_br(fp);
94 } /* is_dos_mbr */
95 
96 int is_dos_f2_mbr(FILE *fp)
97 {
98  #include "mbr_dos_f2.h"
99 
100  return
101  contains_data(fp, 0x0, mbr_dos_f2_0x0, sizeof(mbr_dos_f2_0x0)) &&
102  is_br(fp);
103 } /* is_dos_f2_mbr */
104 
105 int is_95b_mbr(FILE *fp)
106 {
107  #include "mbr_95b.h"
108 
109  return
110  contains_data(fp, 0x0, mbr_95b_0x0, sizeof(mbr_95b_0x0)) &&
111  contains_data(fp, 0x0e0, mbr_95b_0x0e0, sizeof(mbr_95b_0x0e0)) &&
112  is_br(fp);
113 } /* is_95b_mbr */
114 
115 int is_2000_mbr(FILE *fp)
116 {
117  #include "mbr_2000.h"
118 
119  return
120  contains_data(fp, 0x0, mbr_2000_0x0, sizeof(mbr_2000_0x0)) &&
121  is_br(fp);
122 } /* is_2000_mbr */
123 
124 int is_vista_mbr(FILE *fp)
125 {
126  #include "mbr_vista.h"
127 
128  return
129  contains_data(fp, 0x0, mbr_vista_0x0, sizeof(mbr_vista_0x0)) &&
130  is_br(fp);
131 } /* is_vista_mbr */
132 
133 int is_win7_mbr(FILE *fp)
134 {
135  #include "mbr_win7.h"
136 
137  return
138  contains_data(fp, 0x0, mbr_win7_0x0, sizeof(mbr_win7_0x0)) &&
139  is_br(fp);
140 } /* is_win7_mbr */
141 
142 int is_rufus_mbr(FILE *fp)
143 {
144  #include "mbr_rufus.h"
145 
146  return
147  contains_data(fp, 0x0, mbr_rufus_0x0, sizeof(mbr_rufus_0x0)) &&
148  is_br(fp);
149 } /* is_rufus_mbr */
150 
151 int is_reactos_mbr(FILE *fp)
152 {
153  #include "mbr_reactos.h"
154 
155  return
156  contains_data(fp, 0x0, mbr_reactos_0x0, sizeof(mbr_reactos_0x0)) &&
157  is_br(fp);
158 } /* is_reactos_mbr */
159 
160 int is_grub4dos_mbr(FILE *fp)
161 {
162  #include "mbr_grub.h"
163 
164  return
165  contains_data(fp, 0x0, mbr_grub_0x0, sizeof(mbr_grub_0x0)) &&
166  is_br(fp);
167 } /* is_grub_mbr */
168 
169 int is_grub2_mbr(FILE *fp)
170 {
171  #include "mbr_grub2.h"
172 
173  return
174  contains_data(fp, 0x0, mbr_grub2_0x0, sizeof(mbr_grub2_0x0)) &&
175  is_br(fp);
176 } /* is_grub2_mbr */
177 
178 int is_kolibrios_mbr(FILE *fp)
179 {
180  #include "mbr_kolibri.h"
181 
182  return
183  contains_data(fp, 0x0, mbr_kolibri_0x0, sizeof(mbr_kolibri_0x0)) &&
184  is_br(fp);
185 } /* is_kolibri_mbr */
186 
187 int is_syslinux_mbr(FILE *fp)
188 {
189  #include "mbr_syslinux.h"
190 
191  return
192  contains_data(fp, 0x0, mbr_syslinux_0x0, sizeof(mbr_syslinux_0x0)) &&
193  is_br(fp);
194 } /* is_syslinux_mbr */
195 
196 int is_syslinux_gpt_mbr(FILE *fp)
197 {
198  #include "mbr_gpt_syslinux.h"
199 
200  return
202  sizeof(mbr_gpt_syslinux_0x0)) &&
203  is_br(fp);
204 } /* is_syslinux_gpt_mbr */
205 
206 int is_zero_mbr(FILE *fp)
207 {
208  #include "mbr_zero.h"
209 
210  return
211  contains_data(fp, 0x0, mbr_zero_0x0, sizeof(mbr_zero_0x0));
212  /* Don't bother to check 55AA signature */
213 } /* is_zero_mbr */
214 
216 {
217  #include "mbr_zero.h"
218 
219  return
220  contains_data(fp, 0x0, mbr_zero_0x0, 0x1b8);
221 } /* is_zero_mbr_not_including_disk_signature_or_copy_protect */
222 
223 /* Handle nonstandard sector sizes (such as 4K) by writing
224  the boot marker at every 512-2 bytes location */
225 static int write_bootmark(FILE *fp)
226 {
227  unsigned char aucRef[] = {0x55, 0xAA};
228  unsigned long pos = 0x1FE;
229 
230  for (pos = 0x1FE; pos < ulBytesPerSector; pos += 0x200) {
231  if (!write_data(fp, pos, aucRef, sizeof(aucRef)))
232  return 0;
233  }
234  return 1;
235 }
236 
237 int write_dos_mbr(FILE *fp)
238 {
239  #include "mbr_dos.h"
240 
241  return
242  write_data(fp, 0x0, mbr_dos_0x0, sizeof(mbr_dos_0x0)) &&
243  write_bootmark(fp);
244 } /* write_dos_mbr */
245 
246 int write_95b_mbr(FILE *fp)
247 {
248  #include "mbr_95b.h"
249 
250  return
251  write_data(fp, 0x0, mbr_95b_0x0, sizeof(mbr_95b_0x0)) &&
252  write_data(fp, 0x0e0, mbr_95b_0x0e0, sizeof(mbr_95b_0x0e0)) &&
253  write_bootmark(fp);
254 } /* write_95b_mbr */
255 
256 int write_2000_mbr(FILE *fp)
257 {
258  #include "mbr_2000.h"
259 
260  return
261  write_data(fp, 0x0, mbr_2000_0x0, sizeof(mbr_2000_0x0)) &&
262  write_bootmark(fp);
263 } /* write_2000_mbr */
264 
265 int write_vista_mbr(FILE *fp)
266 {
267  #include "mbr_vista.h"
268 
269  return
270  write_data(fp, 0x0, mbr_vista_0x0, sizeof(mbr_vista_0x0)) &&
271  write_bootmark(fp);
272 } /* write_vista_mbr */
273 
274 int write_win7_mbr(FILE *fp)
275 {
276  #include "mbr_win7.h"
277 
278  return
279  write_data(fp, 0x0, mbr_win7_0x0, sizeof(mbr_win7_0x0)) &&
280  write_bootmark(fp);
281 } /* write_win7_mbr */
282 
283 int write_rufus_mbr(FILE *fp)
284 {
285  #include "mbr_rufus.h"
286 
287  return
288  write_data(fp, 0x0, mbr_rufus_0x0, sizeof(mbr_rufus_0x0)) &&
289  write_bootmark(fp);
290 } /* write_rufus_mbr */
291 
292 int write_reactos_mbr(FILE *fp)
293 {
294  #include "mbr_reactos.h"
295 
296  return
297  write_data(fp, 0x0, mbr_reactos_0x0, sizeof(mbr_reactos_0x0)) &&
298  write_bootmark(fp);
299 } /* write_reactos_mbr */
300 
301 int write_kolibrios_mbr(FILE *fp)
302 {
303  #include "mbr_kolibri.h"
304 
305  return
306  write_data(fp, 0x0, mbr_kolibri_0x0, sizeof(mbr_kolibri_0x0)) &&
307  write_bootmark(fp);
308 } /* write_kolibri_mbr */
309 
310 int write_syslinux_mbr(FILE *fp)
311 {
312  #include "mbr_syslinux.h"
313 
314  return
315  write_data(fp, 0x0, mbr_syslinux_0x0, sizeof(mbr_syslinux_0x0)) &&
316  write_bootmark(fp);
317 } /* write_syslinux_mbr */
318 
320 {
321  #include "mbr_gpt_syslinux.h"
322 
323  return
325  write_bootmark(fp);
326 } /* write_syslinux_gpt_mbr */
327 
328 int write_grub4dos_mbr(FILE *fp)
329 {
330  #include "mbr_grub.h"
331 
332  return
333  write_data(fp, 0x0, mbr_grub_0x0, sizeof(mbr_grub_0x0)) &&
334  write_bootmark(fp);
335 }
336 
337 int write_grub2_mbr(FILE *fp)
338 {
339  #include "mbr_grub2.h"
340 
341  return
342  write_data(fp, 0x0, mbr_grub2_0x0, sizeof(mbr_grub2_0x0)) &&
343  write_bootmark(fp);
344 }
345 
346 int write_zero_mbr(FILE *fp)
347 {
348  #include "mbr_zero.h"
349 
350  return
351  write_data(fp, 0x0, mbr_zero_0x0, sizeof(mbr_zero_0x0)) &&
352  write_bootmark(fp);
353 } /* write_zero_mbr */
is_lilo_br
int is_lilo_br(FILE *fp)
Definition: br.c:77
write_data
int write_data(FILE *fp, unsigned long ulPosition, const void *pData, unsigned int uiLen)
Definition: file.c:78
nls.h
mbr_rufus.h
is_zero_mbr
int is_zero_mbr(FILE *fp)
Definition: br.c:206
write_zero_mbr
int write_zero_mbr(FILE *fp)
Definition: br.c:346
read_data
int read_data(FILE *fp, unsigned long ulPosition, void *pData, unsigned int uiLen)
Definition: file.c:41
mbr_grub2_0x0
unsigned char mbr_grub2_0x0[]
Definition: mbr_grub2.h:9
mbr_2000.h
mbr_95b_0x0e0
unsigned char mbr_95b_0x0e0[]
Definition: mbr_95b.h:29
is_reactos_mbr
int is_reactos_mbr(FILE *fp)
Definition: br.c:151
mbr_reactos_0x0
unsigned char mbr_reactos_0x0[]
Definition: mbr_reactos.h:2
mbr_dos_0x0
unsigned char mbr_dos_0x0[]
Definition: mbr_dos.h:2
write_95b_mbr
int write_95b_mbr(FILE *fp)
Definition: br.c:246
read_mbr_copy_protect_bytes_explained
const char * read_mbr_copy_protect_bytes_explained(FILE *fp)
Definition: br.c:54
mbr_rufus_0x0
unsigned char mbr_rufus_0x0[]
Definition: mbr_rufus.h:6
mbr_reactos.h
is_95b_mbr
int is_95b_mbr(FILE *fp)
Definition: br.c:105
mbr_kolibri_0x0
unsigned char mbr_kolibri_0x0[]
Definition: mbr_kolibri.h:2
mbr_win7.h
set_bytes_per_sector
void set_bytes_per_sector(unsigned long ulValue)
Definition: br.c:26
mbr_vista_0x0
unsigned char mbr_vista_0x0[]
Definition: mbr_vista.h:5
mbr_gpt_syslinux.h
mbr_dos.h
contains_data
int contains_data(FILE *fp, unsigned long ulPosition, const void *pData, unsigned int uiLen)
Definition: file.c:29
is_vista_mbr
int is_vista_mbr(FILE *fp)
Definition: br.c:124
mbr_95b.h
is_dos_f2_mbr
int is_dos_f2_mbr(FILE *fp)
Definition: br.c:96
mbr_syslinux_0x0
unsigned char mbr_syslinux_0x0[]
Definition: mbr_syslinux.h:2
is_rufus_mbr
int is_rufus_mbr(FILE *fp)
Definition: br.c:142
is_zero_mbr_not_including_disk_signature_or_copy_protect
int is_zero_mbr_not_including_disk_signature_or_copy_protect(FILE *fp)
Definition: br.c:215
write_syslinux_gpt_mbr
int write_syslinux_gpt_mbr(FILE *fp)
Definition: br.c:319
is_br
int is_br(FILE *fp)
Definition: br.c:68
is_dos_mbr
int is_dos_mbr(FILE *fp)
Definition: br.c:87
write_2000_mbr
int write_2000_mbr(FILE *fp)
Definition: br.c:256
read_windows_disk_signature
uint32_t read_windows_disk_signature(FILE *fp)
Definition: br.c:33
mbr_dos_f2.h
mbr_grub2.h
is_syslinux_mbr
int is_syslinux_mbr(FILE *fp)
Definition: br.c:187
write_rufus_mbr
int write_rufus_mbr(FILE *fp)
Definition: br.c:283
mbr_zero_0x0
unsigned char mbr_zero_0x0[]
Definition: mbr_zero.h:2
write_syslinux_mbr
int write_syslinux_mbr(FILE *fp)
Definition: br.c:310
mbr_grub_0x0
unsigned char mbr_grub_0x0[]
Definition: mbr_grub.h:4
br.h
_
#define _(String)
Definition: nls.h:9
write_grub2_mbr
int write_grub2_mbr(FILE *fp)
Definition: br.c:337
mbr_win7_0x0
unsigned char mbr_win7_0x0[]
Definition: mbr_win7.h:5
write_windows_disk_signature
int write_windows_disk_signature(FILE *fp, uint32_t tWDS)
Definition: br.c:41
write_bootmark
static int write_bootmark(FILE *fp)
Definition: br.c:225
write_grub4dos_mbr
int write_grub4dos_mbr(FILE *fp)
Definition: br.c:328
write_win7_mbr
int write_win7_mbr(FILE *fp)
Definition: br.c:274
mbr_vista.h
write_dos_mbr
int write_dos_mbr(FILE *fp)
Definition: br.c:237
is_grub4dos_mbr
int is_grub4dos_mbr(FILE *fp)
Definition: br.c:160
mbr_syslinux.h
ulBytesPerSector
static unsigned long ulBytesPerSector
Definition: br.c:24
file.h
read_mbr_copy_protect_bytes
uint16_t read_mbr_copy_protect_bytes(FILE *fp)
Definition: br.c:46
mbr_gpt_syslinux_0x0
unsigned char mbr_gpt_syslinux_0x0[]
Definition: mbr_gpt_syslinux.h:2
is_syslinux_gpt_mbr
int is_syslinux_gpt_mbr(FILE *fp)
Definition: br.c:196
write_vista_mbr
int write_vista_mbr(FILE *fp)
Definition: br.c:265
is_2000_mbr
int is_2000_mbr(FILE *fp)
Definition: br.c:115
is_win7_mbr
int is_win7_mbr(FILE *fp)
Definition: br.c:133
mbr_dos_f2_0x0
unsigned char mbr_dos_f2_0x0[]
Definition: mbr_dos_f2.h:2
mbr_2000_0x0
unsigned char mbr_2000_0x0[]
Definition: mbr_2000.h:5
is_grub2_mbr
int is_grub2_mbr(FILE *fp)
Definition: br.c:169
write_kolibrios_mbr
int write_kolibrios_mbr(FILE *fp)
Definition: br.c:301
write_reactos_mbr
int write_reactos_mbr(FILE *fp)
Definition: br.c:292
mbr_zero.h
mbr_95b_0x0
unsigned char mbr_95b_0x0[]
Definition: mbr_95b.h:2
mbr_kolibri.h
mbr_grub.h
is_kolibrios_mbr
int is_kolibrios_mbr(FILE *fp)
Definition: br.c:178