2 * QEMU boot sector testing helpers.
4 * Copyright (c) 2016 Red Hat Inc.
7 * Michael S. Tsirkin <mst@redhat.com>
8 * Victor Kaplansky <victork@redhat.com>
10 * This work is licensed under the terms of the GNU GPL, version 2 or later.
11 * See the COPYING file in the top-level directory.
13 #include "qemu/osdep.h"
14 #include "boot-sector.h"
15 #include "qemu-common.h"
18 #define LOW(x) ((x) & 0xff)
19 #define HIGH(x) ((x) >> 8)
21 #define SIGNATURE 0xdead
22 #define SIGNATURE_OFFSET 0x10
23 #define BOOT_SECTOR_ADDRESS 0x7c00
25 /* Boot sector code: write SIGNATURE into memory,
27 * Q35 machine requires a minimum 0x7e000 bytes disk.
30 static uint8_t boot_sector
[0x7e000] = {
31 /* The first sector will be placed at RAM address 00007C00, and
32 * the BIOS transfers control to 00007C00
35 /* Data Segment register should be initialized, since pxe
36 * boot loader can leave it dirty.
39 /* 7c00: move $0000,%ax */
43 /* 7c03: move %ax,%ds */
47 /* 7c05: mov $0xdead,%ax */
49 [0x06] = LOW(SIGNATURE
),
50 [0x07] = HIGH(SIGNATURE
),
51 /* 7c08: mov %ax,0x7c10 */
53 [0x09] = LOW(BOOT_SECTOR_ADDRESS
+ SIGNATURE_OFFSET
),
54 [0x0a] = HIGH(BOOT_SECTOR_ADDRESS
+ SIGNATURE_OFFSET
),
60 /* 7c0e: jmp 0x7c07=0x7c0f-3 */
63 /* We mov 0xdead here: set value to make debugging easier */
64 [SIGNATURE_OFFSET
] = LOW(0xface),
65 [SIGNATURE_OFFSET
+ 1] = HIGH(0xface),
66 /* End of boot sector marker */
71 /* Create boot disk file. */
72 int boot_sector_init(const char *fname
)
74 FILE *f
= fopen(fname
, "w");
77 fprintf(stderr
, "Couldn't open \"%s\": %s", fname
, strerror(errno
));
81 /* For Open Firmware based system, we can use a Forth script instead */
82 if (strcmp(qtest_get_arch(), "ppc64") == 0) {
83 memset(boot_sector
, ' ', sizeof boot_sector
);
84 sprintf((char *)boot_sector
, "\\ Bootscript\n%x %x c! %x %x c!\n",
85 LOW(SIGNATURE
), BOOT_SECTOR_ADDRESS
+ SIGNATURE_OFFSET
,
86 HIGH(SIGNATURE
), BOOT_SECTOR_ADDRESS
+ SIGNATURE_OFFSET
+ 1);
89 fwrite(boot_sector
, 1, sizeof boot_sector
, f
);
94 /* Loop until signature in memory is OK. */
95 void boot_sector_test(void)
97 uint8_t signature_low
;
98 uint8_t signature_high
;
102 /* Wait at most 1 minute */
103 #define TEST_DELAY (1 * G_USEC_PER_SEC / 10)
104 #define TEST_CYCLES MAX((60 * G_USEC_PER_SEC / TEST_DELAY), 1)
106 /* Poll until code has run and modified memory. Once it has we know BIOS
107 * initialization is done. TODO: check that IP reached the halt
110 for (i
= 0; i
< TEST_CYCLES
; ++i
) {
111 signature_low
= readb(BOOT_SECTOR_ADDRESS
+ SIGNATURE_OFFSET
);
112 signature_high
= readb(BOOT_SECTOR_ADDRESS
+ SIGNATURE_OFFSET
+ 1);
113 signature
= (signature_high
<< 8) | signature_low
;
114 if (signature
== SIGNATURE
) {
117 g_usleep(TEST_DELAY
);
120 g_assert_cmphex(signature
, ==, SIGNATURE
);
123 /* unlink boot disk file. */
124 void boot_sector_cleanup(const char *fname
)