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#define SIGNATURE_ADDR (BOOT_SECTOR_ADDRESS + SIGNATURE_OFFSET)
25
26
27
28
29static uint8_t x86_boot_sector[512] = {
30
31
32
33
34
35
36
37
38
39 [0x00] = 0xb8,
40 [0x01] = 0x00,
41 [0x02] = 0x00,
42
43 [0x03] = 0x8e,
44 [0x04] = 0xd8,
45
46
47 [0x05] = 0xb8,
48 [0x06] = LOW(SIGNATURE),
49 [0x07] = HIGH(SIGNATURE),
50
51 [0x08] = 0xa3,
52 [0x09] = LOW(SIGNATURE_ADDR),
53 [0x0a] = HIGH(SIGNATURE_ADDR),
54
55
56 [0x0b] = 0xfa,
57
58 [0x0c] = 0xf4,
59
60 [0x0d] = 0xeb,
61 [0x0e] = LOW(-3),
62
63 [SIGNATURE_OFFSET] = LOW(0xface),
64 [SIGNATURE_OFFSET + 1] = HIGH(0xface),
65
66 [0x1FE] = 0x55,
67 [0x1FF] = 0xAA,
68};
69
70
71static const uint8_t s390x_psw[] = {
72 0x00, 0x08, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00
73};
74static const uint8_t s390x_code[] = {
75 0xa7, 0xf4, 0x00, 0x0a,
76 0x00, 0x00, 0x00, 0x00,
77 'S', '3', '9', '0',
78 'E', 'P', 0x00, 0x01,
79 0xa7, 0x38, HIGH(SIGNATURE_ADDR), LOW(SIGNATURE_ADDR),
80 0xa7, 0x48, LOW(SIGNATURE), HIGH(SIGNATURE),
81 0x40, 0x40, 0x30, 0x00,
82 0xa7, 0xf4, 0xff, 0xfa
83};
84
85
86int boot_sector_init(char *fname)
87{
88 int fd, ret;
89 size_t len;
90 char *boot_code;
91 const char *arch = qtest_get_arch();
92
93 fd = mkstemp(fname);
94 if (fd < 0) {
95 fprintf(stderr, "Couldn't open \"%s\": %s", fname, strerror(errno));
96 return 1;
97 }
98
99 if (g_str_equal(arch, "i386") || g_str_equal(arch, "x86_64")) {
100
101 len = MAX(0x7e000, sizeof(x86_boot_sector));
102 boot_code = g_malloc0(len);
103 memcpy(boot_code, x86_boot_sector, sizeof(x86_boot_sector));
104 } else if (g_str_equal(arch, "ppc64")) {
105
106 boot_code = g_strdup_printf("\\ Bootscript\n%x %x c! %x %x c!\n",
107 LOW(SIGNATURE), SIGNATURE_ADDR,
108 HIGH(SIGNATURE), SIGNATURE_ADDR + 1);
109 len = strlen(boot_code);
110 } else if (g_str_equal(arch, "s390x")) {
111 len = 0x10000 + sizeof(s390x_code);
112 boot_code = g_malloc0(len);
113 memcpy(boot_code, s390x_psw, sizeof(s390x_psw));
114 memcpy(&boot_code[0x10000], s390x_code, sizeof(s390x_code));
115 } else {
116 g_assert_not_reached();
117 }
118
119 ret = write(fd, boot_code, len);
120 close(fd);
121
122 g_free(boot_code);
123
124 if (ret != len) {
125 fprintf(stderr, "Could not write \"%s\"", fname);
126 return 1;
127 }
128
129 return 0;
130}
131
132
133void boot_sector_test(QTestState *qts)
134{
135 uint8_t signature_low;
136 uint8_t signature_high;
137 uint16_t signature;
138 int i;
139
140
141#define TEST_DELAY (1 * G_USEC_PER_SEC / 10)
142#define TEST_CYCLES MAX((600 * G_USEC_PER_SEC / TEST_DELAY), 1)
143
144
145
146
147
148 for (i = 0; i < TEST_CYCLES; ++i) {
149 signature_low = qtest_readb(qts, SIGNATURE_ADDR);
150 signature_high = qtest_readb(qts, SIGNATURE_ADDR + 1);
151 signature = (signature_high << 8) | signature_low;
152 if (signature == SIGNATURE) {
153 break;
154 }
155 g_usleep(TEST_DELAY);
156 }
157
158 g_assert_cmphex(signature, ==, SIGNATURE);
159}
160
161
162void boot_sector_cleanup(const char *fname)
163{
164 unlink(fname);
165}
166