Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the coreboot project. |
| 3 | * |
| 4 | * Copyright (C) 2016 Google Inc |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; version 2 of the License. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | */ |
| 15 | |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 16 | #include <stdio.h> |
| 17 | #include <stdlib.h> |
| 18 | #include <unistd.h> |
| 19 | #include <sys/io.h> |
| 20 | #include <sys/mman.h> |
| 21 | #include <fcntl.h> |
| 22 | #include <getopt.h> |
Richard Smith | ff8c08e | 2006-04-23 19:16:09 +0000 | [diff] [blame] | 23 | #include <string.h> |
Stefan Reinauer | 850e7d4 | 2015-09-28 13:12:04 -0700 | [diff] [blame] | 24 | #include <stdarg.h> |
Stefan Reinauer | 850e7d4 | 2015-09-28 13:12:04 -0700 | [diff] [blame] | 25 | #include <stdtypes.h> |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 26 | #define die(x) { perror(x); exit(1); } |
Ronald G. Minnich | 284c27f | 2004-11-28 04:39:45 +0000 | [diff] [blame] | 27 | #define warn(x) { perror(x); } |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 28 | |
Stefan Reinauer | 86051f9 | 2010-03-23 15:53:38 +0000 | [diff] [blame] | 29 | #include <x86emu/x86emu.h> |
Stefan Reinauer | 850e7d4 | 2015-09-28 13:12:04 -0700 | [diff] [blame] | 30 | #include <console/console.h> |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 31 | #include <arch/byteorder.h> |
| 32 | #include "device.h" |
| 33 | |
Stefan Reinauer | 850e7d4 | 2015-09-28 13:12:04 -0700 | [diff] [blame] | 34 | #include "testbios.h" |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 35 | #include "pci-userspace.h" |
Stefan Reinauer | 850e7d4 | 2015-09-28 13:12:04 -0700 | [diff] [blame] | 36 | int X86EMU_set_debug(int debug); |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 37 | |
| 38 | biosemu_device_t bios_device; |
Richard Smith | ff8c08e | 2006-04-23 19:16:09 +0000 | [diff] [blame] | 39 | |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 40 | extern int teststart, testend; |
| 41 | |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 42 | #define BIOSMEM_SIZE (1024 * 1024) |
| 43 | unsigned char biosmem[BIOSMEM_SIZE]; |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 44 | |
| 45 | int verbose = 0; |
| 46 | |
Stefan Reinauer | 850e7d4 | 2015-09-28 13:12:04 -0700 | [diff] [blame] | 47 | static unsigned char *mapitin(char *file, off_t where, size_t size) |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 48 | { |
| 49 | void *z; |
| 50 | |
| 51 | int fd = open(file, O_RDWR, 0); |
| 52 | |
| 53 | if (fd < 0) |
| 54 | die(file); |
| 55 | z = mmap(0, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, where); |
| 56 | if (z == (void *) -1) |
| 57 | die("mmap"); |
| 58 | close(fd); |
| 59 | |
| 60 | return z; |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 63 | static unsigned short get_device(char *arg_val) |
| 64 | { |
| 65 | unsigned short devfn=0; |
| 66 | long bus=0,dev=0,fn=0,need_pack=0; |
| 67 | char *tok; |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 68 | |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 69 | tok = strsep(&arg_val,":"); |
| 70 | if (arg_val != NULL) { |
| 71 | bus = strtol(tok,0,16); |
| 72 | need_pack = 1; |
| 73 | } |
| 74 | else { |
| 75 | arg_val = tok; |
| 76 | } |
| 77 | |
| 78 | tok = strsep(&arg_val,"."); |
| 79 | if (arg_val != NULL) { |
| 80 | dev = strtol(tok,0,16); |
| 81 | fn = strtol(arg_val,0,16); |
| 82 | need_pack = 1; |
| 83 | } |
| 84 | else { |
| 85 | if (need_pack ==1 && (strlen(tok))) { |
| 86 | dev = strtol(tok,0,16); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | if ( need_pack == 1) { |
| 91 | devfn = bus<<8 | (dev<<3) | fn; |
| 92 | } |
| 93 | else { |
| 94 | devfn = strtol(tok, 0, 0); |
| 95 | } |
| 96 | |
| 97 | return devfn; |
| 98 | } |
| 99 | |
| 100 | int printk(int msg_level, const char *fmt, ...) |
| 101 | { |
| 102 | va_list args; |
| 103 | int i; |
| 104 | |
| 105 | printf ("<%d> ", msg_level); |
| 106 | va_start(args, fmt); |
| 107 | i = vprintf(fmt, args); |
| 108 | va_end(args); |
| 109 | |
| 110 | return i; |
| 111 | } |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 112 | |
Stefan Reinauer | 850e7d4 | 2015-09-28 13:12:04 -0700 | [diff] [blame] | 113 | static void usage(char *name) |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 114 | { |
| 115 | printf |
| 116 | ("Usage: %s [-c codesegment] [-s size] [-b base] [-i ip] [-t] <filename> ... \n", |
| 117 | name); |
| 118 | } |
| 119 | |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 120 | /* main entry into YABEL biosemu, arguments are: |
| 121 | * *biosmem = pointer to virtual memory |
| 122 | * biosmem_size = size of the virtual memory |
| 123 | * *dev = pointer to the device to be initialised |
| 124 | * rom_addr = address of the OptionROM to be executed, if this is = 0, YABEL |
| 125 | * will look for an ExpansionROM BAR and use the code from there. |
| 126 | */ |
| 127 | u32 |
| 128 | biosemu(u8 *biosmem, u32 biosmem_size, struct device * dev, unsigned long |
| 129 | rom_addr); |
| 130 | |
| 131 | |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 132 | int main(int argc, char **argv) |
| 133 | { |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 134 | int ret; |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 135 | char *absegname = 0; |
| 136 | void *abseg = 0; |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 137 | int c, trace = 0; |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 138 | unsigned char *cp; |
| 139 | char *filename; |
Stefan Reinauer | 850e7d4 | 2015-09-28 13:12:04 -0700 | [diff] [blame] | 140 | ssize_t size = 0; |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 141 | int base = 0; |
| 142 | int have_size = 0, have_base = 0, have_ip = 0, have_cs = 0; |
Richard Smith | ff8c08e | 2006-04-23 19:16:09 +0000 | [diff] [blame] | 143 | int have_devfn = 0; |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 144 | int parse_rom = 0; |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 145 | //char *fsegname = 0; |
| 146 | //unsigned char *fsegptr; |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 147 | unsigned short initialip = 0, initialcs = 0, devfn = 0; |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 148 | //X86EMU_intrFuncs intFuncs[256]; |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 149 | int debugflag = 0; |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 150 | struct device *dev; |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 151 | |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 152 | //const char *optstring = "vh?b:i:c:s:tpd:"; |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 153 | const char *optstring = "vh?b:i:c:s:tpd:"; |
| 154 | while (1) { |
| 155 | int option_index = 0; |
| 156 | static struct option long_options[] = { |
| 157 | {"verbose", 0, 0, 'v'}, |
| 158 | {"help", 0, 0, 'h'}, |
| 159 | {"trace", 0, 0, 't'}, |
| 160 | {"base", 1, 0, 'b'}, |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 161 | //{"fseg", 1, 0, 'f'}, |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 162 | {"instructionpointer", 1, 0, 'i'}, |
| 163 | {"codesegment", 1, 0, 'c'}, |
| 164 | {"absegment", 1, 0, 'a'}, |
| 165 | {"size", 1, 0, 's'}, |
| 166 | {"parserom", 0, 0, 'p'}, |
| 167 | {"device", 1, 0, 'd'}, |
| 168 | {"debug", 1, 0, 'D'}, |
| 169 | {0, 0, 0, 0} |
| 170 | }; |
| 171 | c = getopt_long(argc, argv, optstring, long_options, &option_index); |
| 172 | if (c == -1) |
| 173 | break; |
| 174 | switch (c) { |
| 175 | case 'v': |
| 176 | verbose = 1; |
| 177 | break; |
| 178 | case 'h': |
| 179 | case '?': |
| 180 | usage(argv[0]); |
| 181 | return 0; |
| 182 | case 't': |
| 183 | trace = 1; |
| 184 | break; |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 185 | //case 'b': |
| 186 | // base = strtol(optarg, 0, 0); |
| 187 | // have_base = 1; |
| 188 | // break; |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 189 | case 'i': |
| 190 | initialip = strtol(optarg, 0, 0); |
| 191 | have_ip = 1; |
| 192 | break; |
| 193 | case 'c': |
| 194 | initialcs = strtol(optarg, 0, 0); |
| 195 | have_cs = 1; |
| 196 | break; |
| 197 | case 's': |
| 198 | size = strtol(optarg, 0, 0); |
| 199 | have_size = 1; |
| 200 | break; |
| 201 | case 'p': |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 202 | parse_rom = 1; |
| 203 | break; |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 204 | // case 'f': |
| 205 | // fsegname = optarg; |
| 206 | // break; |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 207 | case 'a': |
| 208 | absegname = optarg; |
| 209 | break; |
| 210 | case 'd': |
Richard Smith | ff8c08e | 2006-04-23 19:16:09 +0000 | [diff] [blame] | 211 | devfn = get_device(optarg); |
| 212 | have_devfn = 1; |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 213 | break; |
| 214 | case 'D': |
| 215 | debugflag = strtol(optarg, 0, 0); |
| 216 | break; |
| 217 | default: |
| 218 | printf("Unknown option \n"); |
| 219 | usage(argv[0]); |
| 220 | return 1; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | if (optind >= argc) { |
| 225 | printf("Filename missing.\n"); |
| 226 | usage(argv[0]); |
| 227 | return 1; |
| 228 | } |
| 229 | |
| 230 | while (optind < argc) { |
| 231 | printf("running file %s\n", argv[optind]); |
| 232 | filename = argv[optind]; |
| 233 | optind++; |
| 234 | /* normally we would do continue, but for |
| 235 | * now only one filename is supported. |
| 236 | */ |
| 237 | /* continue; */ |
| 238 | break; |
| 239 | } |
| 240 | |
| 241 | if (!have_size) { |
| 242 | printf("No size specified. defaulting to 32k\n"); |
| 243 | size = 32 * 1024; |
| 244 | } |
| 245 | if (!have_base) { |
| 246 | printf("No base specified. defaulting to 0xc0000\n"); |
| 247 | base = 0xc0000; |
| 248 | } |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 249 | //if (!have_cs) { |
| 250 | // printf("No initial code segment specified. defaulting to 0xc000\n"); |
| 251 | // initialcs = 0xc000; |
| 252 | //} |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 253 | if (!have_ip) { |
| 254 | printf |
| 255 | ("No initial instruction pointer specified. defaulting to 0x0003\n"); |
| 256 | initialip = 0x0003; |
| 257 | } |
| 258 | |
Stefan Reinauer | 850e7d4 | 2015-09-28 13:12:04 -0700 | [diff] [blame] | 259 | if (parse_rom) |
| 260 | printf("Parsing rom images not implemented.\n"); |
| 261 | |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 262 | //printf("Point 1 int%x vector at %x\n", 0x42, getIntVect(0x42)); |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 263 | #if 0 |
Richard Smith | ff8c08e | 2006-04-23 19:16:09 +0000 | [diff] [blame] | 264 | if (initialip == 0x0003) { |
| 265 | if ((devfn == 0) || (have_devfn == 0)) { |
| 266 | printf("WARNING! It appears you are trying to run an option ROM.\n"); |
| 267 | printf(" (initial ip = 0x0003)\n"); |
| 268 | if (have_devfn) { |
| 269 | printf(" However, the device you have specified is 0x00\n"); |
| 270 | printf(" It is very unlikely that your device is at this address\n"); |
| 271 | printf(" Please check your -d option\n"); |
| 272 | } |
| 273 | else { |
| 274 | printf(" Please specify a device with -d\n"); |
| 275 | printf(" The default is not likely to work\n"); |
| 276 | } |
| 277 | } |
| 278 | } |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 279 | #endif |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 280 | |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 281 | if (absegname) { |
| 282 | abseg = mapitin(absegname, (off_t) 0xa0000, 0x20000); |
| 283 | if (!abseg) |
| 284 | die(absegname); |
| 285 | } |
| 286 | |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 287 | ioperm(0, 0x400, 1); |
| 288 | |
Ronald G. Minnich | 284c27f | 2004-11-28 04:39:45 +0000 | [diff] [blame] | 289 | if (iopl(3) < 0) { |
| 290 | warn("iopl failed, continuing anyway"); |
| 291 | } |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 292 | |
| 293 | /* Emergency sync ;-) */ |
| 294 | sync(); |
| 295 | sync(); |
| 296 | |
| 297 | /* Setting up interrupt environment. |
| 298 | * basically this means initializing PCI and |
| 299 | * intXX handlers. |
| 300 | */ |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 301 | pci_initialize(); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 302 | |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 303 | #if 0 |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 304 | for (i = 0; i < 256; i++) |
| 305 | intFuncs[i] = do_int; |
| 306 | X86EMU_setupIntrFuncs(intFuncs); |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 307 | #endif |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 308 | cp = mapitin(filename, (off_t) 0, size); |
| 309 | |
Richard Smith | ff8c08e | 2006-04-23 19:16:09 +0000 | [diff] [blame] | 310 | if (devfn) { |
| 311 | printf("Loading ax with BusDevFn = %x\n",devfn); |
| 312 | } |
Stefan Reinauer | 14e2277 | 2010-04-27 06:56:47 +0000 | [diff] [blame] | 313 | |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 314 | #if 0 |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 315 | current->ax = devfn ? devfn : 0xff; |
| 316 | current->dx = 0x80; |
| 317 | // current->ip = 0; |
| 318 | for (i = 0; i < size; i++) |
| 319 | wrb(base + i, cp[i]); |
| 320 | |
| 321 | if (fsegname) { |
| 322 | fsegptr = mapitin(fsegname, (off_t) 0, 0x10000); |
| 323 | for (i = 0; i < 0x10000; i++) |
| 324 | wrb(0xf0000 + i, fsegptr[i]); |
| 325 | } else { |
Stefan Reinauer | 850e7d4 | 2015-09-28 13:12:04 -0700 | [diff] [blame] | 326 | const char *date = "01/01/99"; |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 327 | for (i = i; date[i]; i++) |
| 328 | wrb(0xffff5 + i, date[i]); |
| 329 | wrb(0xffff7, '/'); |
| 330 | wrb(0xffffa, '/'); |
| 331 | } |
| 332 | /* cpu setup */ |
| 333 | X86_AX = devfn ? devfn : 0xff; |
| 334 | X86_DX = 0x80; |
| 335 | X86_EIP = initialip; |
| 336 | X86_CS = initialcs; |
| 337 | |
| 338 | /* Initialize stack and data segment */ |
| 339 | X86_SS = 0x0030; |
| 340 | X86_DS = 0x0040; |
| 341 | X86_SP = 0xfffe; |
| 342 | /* We need a sane way to return from bios |
| 343 | * execution. A hlt instruction and a pointer |
| 344 | * to it, both kept on the stack, will do. |
| 345 | */ |
| 346 | pushw(0xf4f4); /* hlt; hlt */ |
| 347 | pushw(X86_SS); |
| 348 | pushw(X86_SP + 2); |
| 349 | |
| 350 | X86_ES = 0x0000; |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 351 | #endif |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 352 | |
| 353 | if (trace) { |
| 354 | printf("Switching to single step mode.\n"); |
Li-Ta Lo | 8b0356c | 2005-01-11 03:18:39 +0000 | [diff] [blame] | 355 | //X86EMU_trace_on(); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 356 | } |
| 357 | if (debugflag) { |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 358 | printf("Enable Debug = %x.\n",debugflag); |
Li-Ta Lo | 8b0356c | 2005-01-11 03:18:39 +0000 | [diff] [blame] | 359 | //X86EMU_set_debug(debugflag); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 360 | } |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 361 | #if 0 |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 362 | X86EMU_exec(); |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 363 | #endif |
| 364 | |
| 365 | ret = biosemu(biosmem, BIOSMEM_SIZE, dev, base); |
| 366 | |
| 367 | #if 0 |
| 368 | current = &p; |
| 369 | X86EMU_setMemBase(biosmem, sizeof(biosmem)); |
| 370 | M.abseg = (unsigned long)abseg; |
| 371 | X86EMU_setupPioFuncs(&myfuncs); |
| 372 | #endif |
| 373 | |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 374 | /* Cleaning up */ |
Stefan Reinauer | 0508273 | 2015-10-21 13:00:41 -0700 | [diff] [blame] | 375 | pci_exit(); |
Li-Ta Lo | 8152126 | 2004-07-08 17:18:27 +0000 | [diff] [blame] | 376 | |
| 377 | return 0; |
| 378 | } |