Debian Codegraph @

harness/mbr.c

raw
1 #include "harness.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <errno.h>
6
7 static const char *mbr_file = NULL;
8 static unsigned char mbr_data[512] = {0};
9
10 void set_mbr(const char *filename)
11 {
12 if (mbr_file)
13 {
14 fprintf(stderr, "MBR file already specified.\n");
15 exit(1);
16 }
17
18 mbr_file = filename;
19 }
20
21 void mbr_init()
22 {
23 unsigned char *code = (void*)0x7c00;
24 FILE *f;
25 size_t res;
26
27 if (!mbr_file)
28 {
29 fprintf(stderr, "MBR file not specified.\n");
30 exit(1);
31 }
32
33 f = fopen(mbr_file, "rb");
34 if (!f)
35 {
36 fprintf(stderr, "Could not open %s for reading: %s\n", mbr_file,
37 strerror(errno));
38 exit(1);
39 }
40 res = fread(mbr_data, 1, 512, f);
41 if (res != 512)
42 {
43 if (ferror(f))
44 {
45 fprintf(stderr, "Error reading from %s: %s\n", mbr_file,
46 strerror(errno));
47 exit(1);
48 }
49 else
50 {
51 fprintf(stderr, "EOF reading from %s.\n", mbr_file);
52 exit(1);
53 }
54 }
55 fclose(f);
56
57 /* Copy it into the VM. */
58 memcpy(code, mbr_data, sizeof(mbr_data));
59
60 /* Set the program start. */
61 set_pc (0, 0x7c00);
62 }
63
64 void compare_mbr(const unsigned char *data)
65 {
66 unsigned int i;
67 unsigned int last = 1024; /* Force a sync. */
68 for (i=0; i<512; i++)
69 {
70 /* Write out all differences. */
71 if (data[i] != mbr_data[i])
72 {
73 /* Write the address if we want the synchronise. */
74 if ((i-last) >= 16)
75 {
76 outputf("At address 0x%x:\n", i);
77 last = i;
78 }
79 outputf("Changed 0x%02x to 0x%02x.\n", mbr_data[i], data[i]);
80 }
81 else
82 /* Force a sync next time since the sequence is broken. */
83 last = 1024;
84 }
85 }
86
87 /* */
88 /* Local Variables: */
89 /* mode:c */
90 /* c-indentation-style: "whitesmith" */
91 /* c-basic-offset: 2 */
92 /* End: */