Victor Kaplansky | 4e08256 | 2016-02-14 18:59:27 +0200 | [diff] [blame] | 1 | /* |
| 2 | * QEMU boot sector testing helpers. |
| 3 | * |
| 4 | * Copyright (c) 2016 Red Hat Inc. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Michael S. Tsirkin <mst@redhat.com> |
Eric Blake | 8b19f2b | 2017-09-11 12:20:07 -0500 | [diff] [blame] | 8 | * Victor Kaplansky <victork@redhat.com> |
Victor Kaplansky | 4e08256 | 2016-02-14 18:59:27 +0200 | [diff] [blame] | 9 | * |
| 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. |
| 12 | */ |
Peter Maydell | 974dc73 | 2016-02-23 12:00:47 +0000 | [diff] [blame] | 13 | #include "qemu/osdep.h" |
Victor Kaplansky | 4e08256 | 2016-02-14 18:59:27 +0200 | [diff] [blame] | 14 | #include "boot-sector.h" |
Victor Kaplansky | 4e08256 | 2016-02-14 18:59:27 +0200 | [diff] [blame] | 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 |
Thomas Huth | 83898cc | 2017-08-11 07:57:55 +0200 | [diff] [blame] | 24 | #define SIGNATURE_ADDR (BOOT_SECTOR_ADDRESS + SIGNATURE_OFFSET) |
Victor Kaplansky | 4e08256 | 2016-02-14 18:59:27 +0200 | [diff] [blame] | 25 | |
Thomas Huth | 83898cc | 2017-08-11 07:57:55 +0200 | [diff] [blame] | 26 | /* x86 boot sector code: write SIGNATURE into memory, |
Victor Kaplansky | 4e08256 | 2016-02-14 18:59:27 +0200 | [diff] [blame] | 27 | * then halt. |
Victor Kaplansky | 4e08256 | 2016-02-14 18:59:27 +0200 | [diff] [blame] | 28 | */ |
Thomas Huth | 83898cc | 2017-08-11 07:57:55 +0200 | [diff] [blame] | 29 | static uint8_t x86_boot_sector[512] = { |
Victor Kaplansky | 4e08256 | 2016-02-14 18:59:27 +0200 | [diff] [blame] | 30 | /* The first sector will be placed at RAM address 00007C00, and |
| 31 | * the BIOS transfers control to 00007C00 |
| 32 | */ |
| 33 | |
| 34 | /* Data Segment register should be initialized, since pxe |
| 35 | * boot loader can leave it dirty. |
| 36 | */ |
| 37 | |
| 38 | /* 7c00: move $0000,%ax */ |
| 39 | [0x00] = 0xb8, |
| 40 | [0x01] = 0x00, |
| 41 | [0x02] = 0x00, |
| 42 | /* 7c03: move %ax,%ds */ |
| 43 | [0x03] = 0x8e, |
| 44 | [0x04] = 0xd8, |
| 45 | |
| 46 | /* 7c05: mov $0xdead,%ax */ |
| 47 | [0x05] = 0xb8, |
| 48 | [0x06] = LOW(SIGNATURE), |
| 49 | [0x07] = HIGH(SIGNATURE), |
| 50 | /* 7c08: mov %ax,0x7c10 */ |
| 51 | [0x08] = 0xa3, |
Thomas Huth | 83898cc | 2017-08-11 07:57:55 +0200 | [diff] [blame] | 52 | [0x09] = LOW(SIGNATURE_ADDR), |
| 53 | [0x0a] = HIGH(SIGNATURE_ADDR), |
Victor Kaplansky | 4e08256 | 2016-02-14 18:59:27 +0200 | [diff] [blame] | 54 | |
| 55 | /* 7c0b cli */ |
| 56 | [0x0b] = 0xfa, |
| 57 | /* 7c0c: hlt */ |
| 58 | [0x0c] = 0xf4, |
| 59 | /* 7c0e: jmp 0x7c07=0x7c0f-3 */ |
| 60 | [0x0d] = 0xeb, |
| 61 | [0x0e] = LOW(-3), |
| 62 | /* We mov 0xdead here: set value to make debugging easier */ |
| 63 | [SIGNATURE_OFFSET] = LOW(0xface), |
| 64 | [SIGNATURE_OFFSET + 1] = HIGH(0xface), |
| 65 | /* End of boot sector marker */ |
| 66 | [0x1FE] = 0x55, |
| 67 | [0x1FF] = 0xAA, |
| 68 | }; |
| 69 | |
Thomas Huth | b1b2fea | 2017-08-11 07:57:56 +0200 | [diff] [blame] | 70 | /* For s390x, use a mini "kernel" with the appropriate signature */ |
Thomas Huth | b1dd6d2 | 2018-06-08 13:17:39 -0400 | [diff] [blame] | 71 | static const uint8_t s390x_psw_and_magic[] = { |
| 72 | 0x00, 0x08, 0x00, 0x00, 0x80, 0x01, 0x00, 0x00, /* Program status word */ |
| 73 | 0x02, 0x00, 0x00, 0x18, 0x60, 0x00, 0x00, 0x50, /* Magic: */ |
| 74 | 0x02, 0x00, 0x00, 0x68, 0x60, 0x00, 0x00, 0x50, /* see linux_s390_magic */ |
| 75 | 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40 /* in the s390-ccw bios */ |
Thomas Huth | b1b2fea | 2017-08-11 07:57:56 +0200 | [diff] [blame] | 76 | }; |
| 77 | static const uint8_t s390x_code[] = { |
| 78 | 0xa7, 0xf4, 0x00, 0x0a, /* j 0x10010 */ |
| 79 | 0x00, 0x00, 0x00, 0x00, |
| 80 | 'S', '3', '9', '0', |
| 81 | 'E', 'P', 0x00, 0x01, |
| 82 | 0xa7, 0x38, HIGH(SIGNATURE_ADDR), LOW(SIGNATURE_ADDR), /* lhi r3,0x7c10 */ |
| 83 | 0xa7, 0x48, LOW(SIGNATURE), HIGH(SIGNATURE), /* lhi r4,0xadde */ |
| 84 | 0x40, 0x40, 0x30, 0x00, /* sth r4,0(r3) */ |
| 85 | 0xa7, 0xf4, 0xff, 0xfa /* j 0x10010 */ |
| 86 | }; |
| 87 | |
Victor Kaplansky | 4e08256 | 2016-02-14 18:59:27 +0200 | [diff] [blame] | 88 | /* Create boot disk file. */ |
Thomas Huth | 3e35377 | 2016-10-11 17:19:36 +0200 | [diff] [blame] | 89 | int boot_sector_init(char *fname) |
Victor Kaplansky | 4e08256 | 2016-02-14 18:59:27 +0200 | [diff] [blame] | 90 | { |
Thomas Huth | 3e35377 | 2016-10-11 17:19:36 +0200 | [diff] [blame] | 91 | int fd, ret; |
Thomas Huth | 83898cc | 2017-08-11 07:57:55 +0200 | [diff] [blame] | 92 | size_t len; |
| 93 | char *boot_code; |
| 94 | const char *arch = qtest_get_arch(); |
Victor Kaplansky | 4e08256 | 2016-02-14 18:59:27 +0200 | [diff] [blame] | 95 | |
Thomas Huth | 3e35377 | 2016-10-11 17:19:36 +0200 | [diff] [blame] | 96 | fd = mkstemp(fname); |
| 97 | if (fd < 0) { |
Victor Kaplansky | 4e08256 | 2016-02-14 18:59:27 +0200 | [diff] [blame] | 98 | fprintf(stderr, "Couldn't open \"%s\": %s", fname, strerror(errno)); |
| 99 | return 1; |
| 100 | } |
Thomas Huth | 1485ef1 | 2016-09-26 22:17:46 +0200 | [diff] [blame] | 101 | |
Thomas Huth | 83898cc | 2017-08-11 07:57:55 +0200 | [diff] [blame] | 102 | if (g_str_equal(arch, "i386") || g_str_equal(arch, "x86_64")) { |
| 103 | /* Q35 requires a minimum 0x7e000 bytes disk (bug or feature?) */ |
| 104 | len = MAX(0x7e000, sizeof(x86_boot_sector)); |
| 105 | boot_code = g_malloc0(len); |
| 106 | memcpy(boot_code, x86_boot_sector, sizeof(x86_boot_sector)); |
| 107 | } else if (g_str_equal(arch, "ppc64")) { |
| 108 | /* For Open Firmware based system, use a Forth script */ |
| 109 | boot_code = g_strdup_printf("\\ Bootscript\n%x %x c! %x %x c!\n", |
| 110 | LOW(SIGNATURE), SIGNATURE_ADDR, |
| 111 | HIGH(SIGNATURE), SIGNATURE_ADDR + 1); |
| 112 | len = strlen(boot_code); |
Thomas Huth | b1b2fea | 2017-08-11 07:57:56 +0200 | [diff] [blame] | 113 | } else if (g_str_equal(arch, "s390x")) { |
| 114 | len = 0x10000 + sizeof(s390x_code); |
| 115 | boot_code = g_malloc0(len); |
Thomas Huth | b1dd6d2 | 2018-06-08 13:17:39 -0400 | [diff] [blame] | 116 | memcpy(boot_code, s390x_psw_and_magic, sizeof(s390x_psw_and_magic)); |
Thomas Huth | b1b2fea | 2017-08-11 07:57:56 +0200 | [diff] [blame] | 117 | memcpy(&boot_code[0x10000], s390x_code, sizeof(s390x_code)); |
Thomas Huth | 83898cc | 2017-08-11 07:57:55 +0200 | [diff] [blame] | 118 | } else { |
| 119 | g_assert_not_reached(); |
Thomas Huth | 1485ef1 | 2016-09-26 22:17:46 +0200 | [diff] [blame] | 120 | } |
| 121 | |
Thomas Huth | 83898cc | 2017-08-11 07:57:55 +0200 | [diff] [blame] | 122 | ret = write(fd, boot_code, len); |
Thomas Huth | 3e35377 | 2016-10-11 17:19:36 +0200 | [diff] [blame] | 123 | close(fd); |
| 124 | |
Thomas Huth | 83898cc | 2017-08-11 07:57:55 +0200 | [diff] [blame] | 125 | g_free(boot_code); |
| 126 | |
Thomas Huth | 3e35377 | 2016-10-11 17:19:36 +0200 | [diff] [blame] | 127 | if (ret != len) { |
| 128 | fprintf(stderr, "Could not write \"%s\"", fname); |
| 129 | return 1; |
| 130 | } |
| 131 | |
Victor Kaplansky | 4e08256 | 2016-02-14 18:59:27 +0200 | [diff] [blame] | 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | /* Loop until signature in memory is OK. */ |
Eric Blake | 8b19f2b | 2017-09-11 12:20:07 -0500 | [diff] [blame] | 136 | void boot_sector_test(QTestState *qts) |
Victor Kaplansky | 4e08256 | 2016-02-14 18:59:27 +0200 | [diff] [blame] | 137 | { |
| 138 | uint8_t signature_low; |
| 139 | uint8_t signature_high; |
| 140 | uint16_t signature; |
| 141 | int i; |
| 142 | |
Thomas Huth | 0789700 | 2017-09-22 05:06:57 +0200 | [diff] [blame] | 143 | /* Wait at most 600 seconds (test is slow with TCI and --enable-debug) */ |
Victor Kaplansky | 4e08256 | 2016-02-14 18:59:27 +0200 | [diff] [blame] | 144 | #define TEST_DELAY (1 * G_USEC_PER_SEC / 10) |
Thomas Huth | 0789700 | 2017-09-22 05:06:57 +0200 | [diff] [blame] | 145 | #define TEST_CYCLES MAX((600 * G_USEC_PER_SEC / TEST_DELAY), 1) |
Victor Kaplansky | 4e08256 | 2016-02-14 18:59:27 +0200 | [diff] [blame] | 146 | |
| 147 | /* Poll until code has run and modified memory. Once it has we know BIOS |
| 148 | * initialization is done. TODO: check that IP reached the halt |
| 149 | * instruction. |
| 150 | */ |
| 151 | for (i = 0; i < TEST_CYCLES; ++i) { |
Eric Blake | 8b19f2b | 2017-09-11 12:20:07 -0500 | [diff] [blame] | 152 | signature_low = qtest_readb(qts, SIGNATURE_ADDR); |
| 153 | signature_high = qtest_readb(qts, SIGNATURE_ADDR + 1); |
Victor Kaplansky | 4e08256 | 2016-02-14 18:59:27 +0200 | [diff] [blame] | 154 | signature = (signature_high << 8) | signature_low; |
| 155 | if (signature == SIGNATURE) { |
| 156 | break; |
| 157 | } |
| 158 | g_usleep(TEST_DELAY); |
| 159 | } |
| 160 | |
| 161 | g_assert_cmphex(signature, ==, SIGNATURE); |
| 162 | } |
| 163 | |
| 164 | /* unlink boot disk file. */ |
| 165 | void boot_sector_cleanup(const char *fname) |
| 166 | { |
| 167 | unlink(fname); |
| 168 | } |