1
2
3
4
5
6
7
8
9
10
11
12
13#include "qemu/osdep.h"
14#include "boot-sector.h"
15#include "qemu-common.h"
16#include "libqtest.h"
17
18#define LOW(x) ((x) & 0xff)
19#define HIGH(x) ((x) >> 8)
20
21#define SIGNATURE 0xdead
22#define SIGNATURE_OFFSET 0x10
23#define BOOT_SECTOR_ADDRESS 0x7c00
24
25
26
27
28
29
30static uint8_t boot_sector[0x7e000] = {
31
32
33
34
35
36
37
38
39
40 [0x00] = 0xb8,
41 [0x01] = 0x00,
42 [0x02] = 0x00,
43
44 [0x03] = 0x8e,
45 [0x04] = 0xd8,
46
47
48 [0x05] = 0xb8,
49 [0x06] = LOW(SIGNATURE),
50 [0x07] = HIGH(SIGNATURE),
51
52 [0x08] = 0xa3,
53 [0x09] = LOW(BOOT_SECTOR_ADDRESS + SIGNATURE_OFFSET),
54 [0x0a] = HIGH(BOOT_SECTOR_ADDRESS + SIGNATURE_OFFSET),
55
56
57 [0x0b] = 0xfa,
58
59 [0x0c] = 0xf4,
60
61 [0x0d] = 0xeb,
62 [0x0e] = LOW(-3),
63
64 [SIGNATURE_OFFSET] = LOW(0xface),
65 [SIGNATURE_OFFSET + 1] = HIGH(0xface),
66
67 [0x1FE] = 0x55,
68 [0x1FF] = 0xAA,
69};
70
71
72int boot_sector_init(const char *fname)
73{
74 FILE *f = fopen(fname, "w");
75
76 if (!f) {
77 fprintf(stderr, "Couldn't open \"%s\": %s", fname, strerror(errno));
78 return 1;
79 }
80 fwrite(boot_sector, 1, sizeof boot_sector, f);
81 fclose(f);
82 return 0;
83}
84
85
86void boot_sector_test(void)
87{
88 uint8_t signature_low;
89 uint8_t signature_high;
90 uint16_t signature;
91 int i;
92
93
94#define TEST_DELAY (1 * G_USEC_PER_SEC / 10)
95#define TEST_CYCLES MAX((60 * G_USEC_PER_SEC / TEST_DELAY), 1)
96
97
98
99
100
101 for (i = 0; i < TEST_CYCLES; ++i) {
102 signature_low = readb(BOOT_SECTOR_ADDRESS + SIGNATURE_OFFSET);
103 signature_high = readb(BOOT_SECTOR_ADDRESS + SIGNATURE_OFFSET + 1);
104 signature = (signature_high << 8) | signature_low;
105 if (signature == SIGNATURE) {
106 break;
107 }
108 g_usleep(TEST_DELAY);
109 }
110
111 g_assert_cmphex(signature, ==, SIGNATURE);
112}
113
114
115void boot_sector_cleanup(const char *fname)
116{
117 unlink(fname);
118}
119