Commit 5cea8590eaa099be8087f363f80d0e6917382385

Authored by Paul Brook
1 parent abc07545

Use relative path for bios

Look for bios and other support files relative to qemu binary, rather than
a hardcoded prefix.

Signed-off-by: Paul Brook <paul@codesourcery.com>
hw/mips_jazz.c
... ... @@ -118,7 +118,7 @@ void mips_jazz_init (ram_addr_t ram_size,
118 118 const char *cpu_model,
119 119 enum jazz_model_e jazz_model)
120 120 {
121   - char buf[1024];
  121 + char *filename;
122 122 int bios_size, n;
123 123 CPUState *env;
124 124 qemu_irq *rc4030, *i8259;
... ... @@ -161,11 +161,17 @@ void mips_jazz_init (ram_addr_t ram_size,
161 161 /* load the BIOS image. */
162 162 if (bios_name == NULL)
163 163 bios_name = BIOS_FILENAME;
164   - snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
165   - bios_size = load_image_targphys(buf, 0xfff00000LL, MAGNUM_BIOS_SIZE);
  164 + filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  165 + if (filename) {
  166 + bios_size = load_image_targphys(filename, 0xfff00000LL,
  167 + MAGNUM_BIOS_SIZE);
  168 + qemu_free(filename);
  169 + } else {
  170 + bios_size = -1;
  171 + }
166 172 if (bios_size < 0 || bios_size > MAGNUM_BIOS_SIZE) {
167 173 fprintf(stderr, "qemu: Could not load MIPS bios '%s'\n",
168   - buf);
  174 + bios_name);
169 175 exit(1);
170 176 }
171 177  
... ...
hw/mips_malta.c
... ... @@ -758,7 +758,7 @@ void mips_malta_init (ram_addr_t ram_size,
758 758 const char *kernel_filename, const char *kernel_cmdline,
759 759 const char *initrd_filename, const char *cpu_model)
760 760 {
761   - char buf[1024];
  761 + char *filename;
762 762 ram_addr_t ram_offset;
763 763 ram_addr_t bios_offset;
764 764 target_long bios_size;
... ... @@ -846,12 +846,18 @@ void mips_malta_init (ram_addr_t ram_size,
846 846 /* Load a BIOS image. */
847 847 if (bios_name == NULL)
848 848 bios_name = BIOS_FILENAME;
849   - snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
850   - bios_size = load_image_targphys(buf, 0x1fc00000LL, BIOS_SIZE);
  849 + filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  850 + if (filename) {
  851 + bios_size = load_image_targphys(filename, 0x1fc00000LL,
  852 + BIOS_SIZE);
  853 + qemu_free(filename);
  854 + } else {
  855 + bios_size = -1;
  856 + }
851 857 if ((bios_size < 0 || bios_size > BIOS_SIZE) && !kernel_filename) {
852 858 fprintf(stderr,
853 859 "qemu: Could not load MIPS bios '%s', and no -kernel argument was specified\n",
854   - buf);
  860 + bios_name);
855 861 exit(1);
856 862 }
857 863 }
... ...
hw/mips_mipssim.c
... ... @@ -107,7 +107,7 @@ mips_mipssim_init (ram_addr_t ram_size,
107 107 const char *kernel_filename, const char *kernel_cmdline,
108 108 const char *initrd_filename, const char *cpu_model)
109 109 {
110   - char buf[1024];
  110 + char *filename;
111 111 ram_addr_t ram_offset;
112 112 ram_addr_t bios_offset;
113 113 CPUState *env;
... ... @@ -140,13 +140,18 @@ mips_mipssim_init (ram_addr_t ram_size,
140 140 /* Load a BIOS / boot exception handler image. */
141 141 if (bios_name == NULL)
142 142 bios_name = BIOS_FILENAME;
143   - snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
144   - bios_size = load_image_targphys(buf, 0x1fc00000LL, BIOS_SIZE);
  143 + filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  144 + if (filename) {
  145 + bios_size = load_image_targphys(filename, 0x1fc00000LL, BIOS_SIZE);
  146 + qemu_free(filename);
  147 + } else {
  148 + bios_size = -1;
  149 + }
145 150 if ((bios_size < 0 || bios_size > BIOS_SIZE) && !kernel_filename) {
146 151 /* Bail out if we have neither a kernel image nor boot vector code. */
147 152 fprintf(stderr,
148 153 "qemu: Could not load MIPS bios '%s', and no -kernel argument was specified\n",
149   - buf);
  154 + filename);
150 155 exit(1);
151 156 } else {
152 157 /* We have a boot vector start address. */
... ...
hw/mips_r4k.c
... ... @@ -147,7 +147,7 @@ void mips_r4k_init (ram_addr_t ram_size,
147 147 const char *kernel_filename, const char *kernel_cmdline,
148 148 const char *initrd_filename, const char *cpu_model)
149 149 {
150   - char buf[1024];
  150 + char *filename;
151 151 ram_addr_t ram_offset;
152 152 ram_addr_t bios_offset;
153 153 int bios_size;
... ... @@ -196,14 +196,18 @@ void mips_r4k_init (ram_addr_t ram_size,
196 196 run. */
197 197 if (bios_name == NULL)
198 198 bios_name = BIOS_FILENAME;
199   - snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
200   - bios_size = get_image_size(buf);
  199 + filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  200 + if (filename) {
  201 + bios_size = get_image_size(filename);
  202 + } else {
  203 + bios_size = -1;
  204 + }
201 205 if ((bios_size > 0) && (bios_size <= BIOS_SIZE)) {
202 206 bios_offset = qemu_ram_alloc(BIOS_SIZE);
203 207 cpu_register_physical_memory(0x1fc00000, BIOS_SIZE,
204 208 bios_offset | IO_MEM_ROM);
205 209  
206   - load_image_targphys(buf, 0x1fc00000, BIOS_SIZE);
  210 + load_image_targphys(filename, 0x1fc00000, BIOS_SIZE);
207 211 } else if ((index = drive_get_index(IF_PFLASH, 0, 0)) > -1) {
208 212 uint32_t mips_rom = 0x00400000;
209 213 bios_offset = qemu_ram_alloc(mips_rom);
... ... @@ -216,7 +220,10 @@ void mips_r4k_init (ram_addr_t ram_size,
216 220 else {
217 221 /* not fatal */
218 222 fprintf(stderr, "qemu: Warning, could not load MIPS bios '%s'\n",
219   - buf);
  223 + bios_name);
  224 + }
  225 + if (filename) {
  226 + qemu_free(filename);
220 227 }
221 228  
222 229 if (kernel_filename) {
... ...
... ... @@ -603,10 +603,10 @@ static void load_linux(target_phys_addr_t option_rom,
603 603 uint32_t gpr[8];
604 604 uint16_t seg[6];
605 605 uint16_t real_seg;
606   - int setup_size, kernel_size, initrd_size, cmdline_size;
  606 + int setup_size, kernel_size, initrd_size = 0, cmdline_size;
607 607 uint32_t initrd_max;
608 608 uint8_t header[1024];
609   - target_phys_addr_t real_addr, prot_addr, cmdline_addr, initrd_addr;
  609 + target_phys_addr_t real_addr, prot_addr, cmdline_addr, initrd_addr = 0;
610 610 FILE *f, *fi;
611 611  
612 612 /* Align to 16 bytes as a paranoia measure */
... ... @@ -805,14 +805,21 @@ static int load_option_rom(const char *oprom, target_phys_addr_t start,
805 805 target_phys_addr_t end)
806 806 {
807 807 int size;
808   -
809   - size = get_image_size(oprom);
810   - if (size > 0 && start + size > end) {
811   - fprintf(stderr, "Not enough space to load option rom '%s'\n",
812   - oprom);
813   - exit(1);
  808 + char *filename;
  809 +
  810 + filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, oprom);
  811 + if (filename) {
  812 + size = get_image_size(filename);
  813 + if (size > 0 && start + size > end) {
  814 + fprintf(stderr, "Not enough space to load option rom '%s'\n",
  815 + oprom);
  816 + exit(1);
  817 + }
  818 + size = load_image_targphys(filename, start, end - start);
  819 + qemu_free(filename);
  820 + } else {
  821 + size = -1;
814 822 }
815   - size = load_image_targphys(oprom, start, end - start);
816 823 if (size < 0) {
817 824 fprintf(stderr, "Could not load option rom '%s'\n", oprom);
818 825 exit(1);
... ... @@ -830,7 +837,7 @@ static void pc_init1(ram_addr_t ram_size,
830 837 const char *initrd_filename,
831 838 int pci_enabled, const char *cpu_model)
832 839 {
833   - char buf[1024];
  840 + char *filename;
834 841 int ret, linux_boot, i;
835 842 ram_addr_t ram_addr, bios_offset, option_rom_offset;
836 843 ram_addr_t below_4g_mem_size, above_4g_mem_size = 0;
... ... @@ -913,19 +920,26 @@ static void pc_init1(ram_addr_t ram_size,
913 920 /* BIOS load */
914 921 if (bios_name == NULL)
915 922 bios_name = BIOS_FILENAME;
916   - snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
917   - bios_size = get_image_size(buf);
  923 + filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  924 + if (filename) {
  925 + bios_size = get_image_size(filename);
  926 + } else {
  927 + bios_size = -1;
  928 + }
918 929 if (bios_size <= 0 ||
919 930 (bios_size % 65536) != 0) {
920 931 goto bios_error;
921 932 }
922 933 bios_offset = qemu_ram_alloc(bios_size);
923   - ret = load_image(buf, qemu_get_ram_ptr(bios_offset));
  934 + ret = load_image(filename, qemu_get_ram_ptr(bios_offset));
924 935 if (ret != bios_size) {
925 936 bios_error:
926   - fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", buf);
  937 + fprintf(stderr, "qemu: could not load PC BIOS '%s'\n", bios_name);
927 938 exit(1);
928 939 }
  940 + if (filename) {
  941 + qemu_free(filename);
  942 + }
929 943 /* map the last 128KB of the BIOS in ISA space */
930 944 isa_bios_size = bios_size;
931 945 if (isa_bios_size > (128 * 1024))
... ... @@ -941,14 +955,14 @@ static void pc_init1(ram_addr_t ram_size,
941 955 cpu_register_physical_memory(0xc0000, 0x20000, option_rom_offset);
942 956  
943 957 if (using_vga) {
  958 + const char *vgabios_filename;
944 959 /* VGA BIOS load */
945 960 if (cirrus_vga_enabled) {
946   - snprintf(buf, sizeof(buf), "%s/%s", bios_dir,
947   - VGABIOS_CIRRUS_FILENAME);
  961 + vgabios_filename = VGABIOS_CIRRUS_FILENAME;
948 962 } else {
949   - snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_FILENAME);
  963 + vgabios_filename = VGABIOS_FILENAME;
950 964 }
951   - oprom_area_size = load_option_rom(buf, 0xc0000, 0xe0000);
  965 + oprom_area_size = load_option_rom(vgabios_filename, 0xc0000, 0xe0000);
952 966 }
953 967 /* Although video roms can grow larger than 0x8000, the area between
954 968 * 0xc0000 - 0xc8000 is reserved for them. It means we won't be looking
... ...
hw/ppc405_boards.c
... ... @@ -175,7 +175,7 @@ static void ref405ep_init (ram_addr_t ram_size,
175 175 const char *initrd_filename,
176 176 const char *cpu_model)
177 177 {
178   - char buf[1024];
  178 + char *filename;
179 179 ppc4xx_bd_info_t bd;
180 180 CPUPPCState *env;
181 181 qemu_irq *pic;
... ... @@ -236,13 +236,19 @@ static void ref405ep_init (ram_addr_t ram_size,
236 236 #ifdef DEBUG_BOARD_INIT
237 237 printf("Load BIOS from file\n");
238 238 #endif
  239 + bios_offset = qemu_ram_alloc(BIOS_SIZE);
239 240 if (bios_name == NULL)
240 241 bios_name = BIOS_FILENAME;
241   - snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
242   - bios_offset = qemu_ram_alloc(BIOS_SIZE);
243   - bios_size = load_image(buf, qemu_get_ram_ptr(bios_offset));
  242 + filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  243 + if (filename) {
  244 + bios_size = load_image(filename, qemu_get_ram_ptr(bios_offset));
  245 + qemu_free(filename);
  246 + } else {
  247 + bios_size = -1;
  248 + }
244 249 if (bios_size < 0 || bios_size > BIOS_SIZE) {
245   - fprintf(stderr, "qemu: could not load PowerPC bios '%s'\n", buf);
  250 + fprintf(stderr, "qemu: could not load PowerPC bios '%s'\n",
  251 + bios_name);
246 252 exit(1);
247 253 }
248 254 bios_size = (bios_size + 0xfff) & ~0xfff;
... ... @@ -493,7 +499,7 @@ static void taihu_405ep_init(ram_addr_t ram_size,
493 499 const char *initrd_filename,
494 500 const char *cpu_model)
495 501 {
496   - char buf[1024];
  502 + char *filename;
497 503 CPUPPCState *env;
498 504 qemu_irq *pic;
499 505 ram_addr_t bios_offset;
... ... @@ -548,10 +554,15 @@ static void taihu_405ep_init(ram_addr_t ram_size,
548 554 if (bios_name == NULL)
549 555 bios_name = BIOS_FILENAME;
550 556 bios_offset = qemu_ram_alloc(BIOS_SIZE);
551   - snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
552   - bios_size = load_image(buf, qemu_get_ram_ptr(bios_offset));
  557 + filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  558 + if (filename) {
  559 + bios_size = load_image(filename, qemu_get_ram_ptr(bios_offset));
  560 + } else {
  561 + bios_size = -1;
  562 + }
553 563 if (bios_size < 0 || bios_size > BIOS_SIZE) {
554   - fprintf(stderr, "qemu: could not load PowerPC bios '%s'\n", buf);
  564 + fprintf(stderr, "qemu: could not load PowerPC bios '%s'\n",
  565 + bios_name);
555 566 exit(1);
556 567 }
557 568 bios_size = (bios_size + 0xfff) & ~0xfff;
... ...
hw/ppc440_bamboo.c
... ... @@ -34,20 +34,19 @@ static void *bamboo_load_device_tree(target_phys_addr_t addr,
34 34 void *fdt = NULL;
35 35 #ifdef HAVE_FDT
36 36 uint32_t mem_reg_property[] = { 0, 0, ramsize };
37   - char *path;
  37 + char *filename;
38 38 int fdt_size;
39   - int pathlen;
40 39 int ret;
41 40  
42   - pathlen = snprintf(NULL, 0, "%s/%s", bios_dir, BINARY_DEVICE_TREE_FILE) + 1;
43   - path = qemu_malloc(pathlen);
44   -
45   - snprintf(path, pathlen, "%s/%s", bios_dir, BINARY_DEVICE_TREE_FILE);
46   -
47   - fdt = load_device_tree(path, &fdt_size);
48   - free(path);
49   - if (fdt == NULL)
  41 + filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, BINARY_DEVICE_TREE_FILE);
  42 + if (!filename) {
50 43 goto out;
  44 + }
  45 + fdt = load_device_tree(filename, &fdt_size);
  46 + qemu_free(filename);
  47 + if (fdt == NULL) {
  48 + goto out;
  49 + }
51 50  
52 51 /* Manipulate device tree in memory. */
53 52  
... ...
hw/ppc_newworld.c
... ... @@ -93,7 +93,7 @@ static void ppc_core99_init (ram_addr_t ram_size,
93 93 const char *cpu_model)
94 94 {
95 95 CPUState *env = NULL, *envs[MAX_CPUS];
96   - char buf[1024];
  96 + char *filename;
97 97 qemu_irq *pic, **openpic_irqs;
98 98 int unin_memory;
99 99 int linux_boot, i;
... ... @@ -140,24 +140,35 @@ static void ppc_core99_init (ram_addr_t ram_size,
140 140 bios_offset = qemu_ram_alloc(BIOS_SIZE);
141 141 if (bios_name == NULL)
142 142 bios_name = PROM_FILENAME;
143   - snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
  143 + filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
144 144 cpu_register_physical_memory(PROM_ADDR, BIOS_SIZE, bios_offset | IO_MEM_ROM);
145 145  
146 146 /* Load OpenBIOS (ELF) */
147   - bios_size = load_elf(buf, 0, NULL, NULL, NULL);
  147 + if (filename) {
  148 + bios_size = load_elf(filename, 0, NULL, NULL, NULL);
  149 + qemu_free(filename);
  150 + } else {
  151 + bios_size = -1;
  152 + }
148 153 if (bios_size < 0 || bios_size > BIOS_SIZE) {
149   - hw_error("qemu: could not load PowerPC bios '%s'\n", buf);
  154 + hw_error("qemu: could not load PowerPC bios '%s'\n", bios_name);
150 155 exit(1);
151 156 }
152 157  
153 158 /* allocate and load VGA BIOS */
154 159 vga_bios_offset = qemu_ram_alloc(VGA_BIOS_SIZE);
155 160 vga_bios_ptr = qemu_get_ram_ptr(vga_bios_offset);
156   - snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_FILENAME);
157   - vga_bios_size = load_image(buf, vga_bios_ptr + 8);
  161 + filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, VGABIOS_FILENAME);
  162 + if (filename) {
  163 + vga_bios_size = load_image(filename, vga_bios_ptr + 8);
  164 + qemu_free(filename);
  165 + } else {
  166 + vga_bios_size = -1;
  167 + }
158 168 if (vga_bios_size < 0) {
159 169 /* if no bios is present, we can still work */
160   - fprintf(stderr, "qemu: warning: could not load VGA bios '%s'\n", buf);
  170 + fprintf(stderr, "qemu: warning: could not load VGA bios '%s'\n",
  171 + VGABIOS_FILENAME);
161 172 vga_bios_size = 0;
162 173 } else {
163 174 /* set a specific header (XXX: find real Apple format for NDRV
... ...
hw/ppc_oldworld.c
... ... @@ -122,7 +122,7 @@ static void ppc_heathrow_init (ram_addr_t ram_size,
122 122 const char *cpu_model)
123 123 {
124 124 CPUState *env = NULL, *envs[MAX_CPUS];
125   - char buf[1024];
  125 + char *filename;
126 126 qemu_irq *pic, **heathrow_irqs;
127 127 int linux_boot, i;
128 128 ram_addr_t ram_offset, bios_offset, vga_bios_offset;
... ... @@ -173,24 +173,35 @@ static void ppc_heathrow_init (ram_addr_t ram_size,
173 173 bios_offset = qemu_ram_alloc(BIOS_SIZE);
174 174 if (bios_name == NULL)
175 175 bios_name = PROM_FILENAME;
176   - snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
  176 + filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
177 177 cpu_register_physical_memory(PROM_ADDR, BIOS_SIZE, bios_offset | IO_MEM_ROM);
178 178  
179 179 /* Load OpenBIOS (ELF) */
180   - bios_size = load_elf(buf, 0, NULL, NULL, NULL);
  180 + if (filename) {
  181 + bios_size = load_elf(filename, 0, NULL, NULL, NULL);
  182 + qemu_free(filename);
  183 + } else {
  184 + bios_size = -1;
  185 + }
181 186 if (bios_size < 0 || bios_size > BIOS_SIZE) {
182   - hw_error("qemu: could not load PowerPC bios '%s'\n", buf);
  187 + hw_error("qemu: could not load PowerPC bios '%s'\n", bios_name);
183 188 exit(1);
184 189 }
185 190  
186 191 /* allocate and load VGA BIOS */
187 192 vga_bios_offset = qemu_ram_alloc(VGA_BIOS_SIZE);
188 193 vga_bios_ptr = qemu_get_ram_ptr(vga_bios_offset);
189   - snprintf(buf, sizeof(buf), "%s/%s", bios_dir, VGABIOS_FILENAME);
190   - vga_bios_size = load_image(buf, vga_bios_ptr + 8);
  194 + filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, VGABIOS_FILENAME);
  195 + if (filename) {
  196 + vga_bios_size = load_image(filename, vga_bios_ptr + 8);
  197 + qemu_free(filename);
  198 + } else {
  199 + vga_bios_size = -1;
  200 + }
191 201 if (vga_bios_size < 0) {
192 202 /* if no bios is present, we can still work */
193   - fprintf(stderr, "qemu: warning: could not load VGA bios '%s'\n", buf);
  203 + fprintf(stderr, "qemu: warning: could not load VGA bios '%s'\n",
  204 + VGABIOS_FILENAME);
194 205 vga_bios_size = 0;
195 206 } else {
196 207 /* set a specific header (XXX: find real Apple format for NDRV
... ...
hw/ppc_prep.c
... ... @@ -539,7 +539,7 @@ static void ppc_prep_init (ram_addr_t ram_size,
539 539 const char *cpu_model)
540 540 {
541 541 CPUState *env = NULL, *envs[MAX_CPUS];
542   - char buf[1024];
  542 + char *filename;
543 543 nvram_t nvram;
544 544 m48t59_t *m48t59;
545 545 int PPC_io_memory;
... ... @@ -585,18 +585,25 @@ static void ppc_prep_init (ram_addr_t ram_size,
585 585 bios_offset = qemu_ram_alloc(BIOS_SIZE);
586 586 if (bios_name == NULL)
587 587 bios_name = BIOS_FILENAME;
588   - snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
589   - bios_size = get_image_size(buf);
  588 + filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  589 + if (filename) {
  590 + bios_size = get_image_size(filename);
  591 + } else {
  592 + bios_size = -1;
  593 + }
590 594 if (bios_size > 0 && bios_size <= BIOS_SIZE) {
591 595 target_phys_addr_t bios_addr;
592 596 bios_size = (bios_size + 0xfff) & ~0xfff;
593 597 bios_addr = (uint32_t)(-bios_size);
594 598 cpu_register_physical_memory(bios_addr, bios_size,
595 599 bios_offset | IO_MEM_ROM);
596   - bios_size = load_image_targphys(buf, bios_addr, bios_size);
  600 + bios_size = load_image_targphys(filename, bios_addr, bios_size);
597 601 }
598 602 if (bios_size < 0 || bios_size > BIOS_SIZE) {
599   - hw_error("qemu: could not load PPC PREP bios '%s'\n", buf);
  603 + hw_error("qemu: could not load PPC PREP bios '%s'\n", bios_name);
  604 + }
  605 + if (filename) {
  606 + qemu_free(filename);
600 607 }
601 608 if (env->nip < 0xFFF80000 && bios_size < 0x00100000) {
602 609 hw_error("PowerPC 601 / 620 / 970 need a 1MB BIOS\n");
... ...
hw/ppce500_mpc8544ds.c
... ... @@ -79,20 +79,19 @@ static void *mpc8544_load_device_tree(target_phys_addr_t addr,
79 79 void *fdt = NULL;
80 80 #ifdef HAVE_FDT
81 81 uint32_t mem_reg_property[] = {0, ramsize};
82   - char *path;
  82 + char *filename;
83 83 int fdt_size;
84   - int pathlen;
85 84 int ret;
86 85  
87   - pathlen = snprintf(NULL, 0, "%s/%s", bios_dir, BINARY_DEVICE_TREE_FILE) + 1;
88   - path = qemu_malloc(pathlen);
89   -
90   - snprintf(path, pathlen, "%s/%s", bios_dir, BINARY_DEVICE_TREE_FILE);
91   -
92   - fdt = load_device_tree(path, &fdt_size);
93   - qemu_free(path);
94   - if (fdt == NULL)
  86 + filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, BINARY_DEVICE_TREE_FILE);
  87 + if (!filename) {
95 88 goto out;
  89 + }
  90 + fdt = load_device_tree(filename, &fdt_size);
  91 + qemu_free(filename);
  92 + if (fdt == NULL) {
  93 + goto out;
  94 + }
96 95  
97 96 /* Manipulate device tree in memory. */
98 97 ret = qemu_devtree_setprop(fdt, "/memory", "reg", mem_reg_property,
... ...
hw/sun4m.c
... ... @@ -400,7 +400,7 @@ static void sun4m_hw_init(const struct sun4m_hwdef *hwdef, ram_addr_t RAM_size,
400 400 ram_addr_t ram_offset, prom_offset, idreg_offset;
401 401 unsigned long kernel_size;
402 402 int ret;
403   - char buf[1024];
  403 + char *filename;
404 404 BlockDriverState *fd[MAX_FD];
405 405 int drive_index;
406 406 void *fw_cfg;
... ... @@ -451,13 +451,20 @@ static void sun4m_hw_init(const struct sun4m_hwdef *hwdef, ram_addr_t RAM_size,
451 451  
452 452 if (bios_name == NULL)
453 453 bios_name = PROM_FILENAME;
454   - snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
455   - ret = load_elf(buf, hwdef->slavio_base - PROM_VADDR, NULL, NULL, NULL);
456   - if (ret < 0 || ret > PROM_SIZE_MAX)
457   - ret = load_image_targphys(buf, hwdef->slavio_base, PROM_SIZE_MAX);
  454 + filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  455 + if (filename) {
  456 + ret = load_elf(filename, hwdef->slavio_base - PROM_VADDR,
  457 + NULL, NULL, NULL);
  458 + if (ret < 0 || ret > PROM_SIZE_MAX)
  459 + ret = load_image_targphys(filename, hwdef->slavio_base,
  460 + PROM_SIZE_MAX);
  461 + qemu_free(filename);
  462 + } else {
  463 + ret = -1;
  464 + }
458 465 if (ret < 0 || ret > PROM_SIZE_MAX) {
459 466 fprintf(stderr, "qemu: could not load prom '%s'\n",
460   - buf);
  467 + bios_name);
461 468 exit(1);
462 469 }
463 470  
... ... @@ -1185,7 +1192,7 @@ static void sun4d_hw_init(const struct sun4d_hwdef *hwdef, ram_addr_t RAM_size,
1185 1192 ram_addr_t ram_offset, prom_offset;
1186 1193 unsigned long kernel_size;
1187 1194 int ret;
1188   - char buf[1024];
  1195 + char *filename;
1189 1196 void *fw_cfg;
1190 1197  
1191 1198 /* init CPUs */
... ... @@ -1233,13 +1240,20 @@ static void sun4d_hw_init(const struct sun4d_hwdef *hwdef, ram_addr_t RAM_size,
1233 1240  
1234 1241 if (bios_name == NULL)
1235 1242 bios_name = PROM_FILENAME;
1236   - snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
1237   - ret = load_elf(buf, hwdef->slavio_base - PROM_VADDR, NULL, NULL, NULL);
1238   - if (ret < 0 || ret > PROM_SIZE_MAX)
1239   - ret = load_image_targphys(buf, hwdef->slavio_base, PROM_SIZE_MAX);
  1243 + filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  1244 + if (filename) {
  1245 + ret = load_elf(filename, hwdef->slavio_base - PROM_VADDR,
  1246 + NULL, NULL, NULL);
  1247 + if (ret < 0 || ret > PROM_SIZE_MAX)
  1248 + ret = load_image_targphys(filename, hwdef->slavio_base,
  1249 + PROM_SIZE_MAX);
  1250 + qemu_free(filename);
  1251 + } else {
  1252 + ret = -1;
  1253 + }
1240 1254 if (ret < 0 || ret > PROM_SIZE_MAX) {
1241 1255 fprintf(stderr, "qemu: could not load prom '%s'\n",
1242   - buf);
  1256 + bios_name);
1243 1257 exit(1);
1244 1258 }
1245 1259  
... ... @@ -1399,7 +1413,7 @@ static void sun4c_hw_init(const struct sun4c_hwdef *hwdef, ram_addr_t RAM_size,
1399 1413 ram_addr_t ram_offset, prom_offset;
1400 1414 unsigned long kernel_size;
1401 1415 int ret;
1402   - char buf[1024];
  1416 + char *filename;
1403 1417 BlockDriverState *fd[MAX_FD];
1404 1418 int drive_index;
1405 1419 void *fw_cfg;
... ... @@ -1440,13 +1454,20 @@ static void sun4c_hw_init(const struct sun4c_hwdef *hwdef, ram_addr_t RAM_size,
1440 1454  
1441 1455 if (bios_name == NULL)
1442 1456 bios_name = PROM_FILENAME;
1443   - snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
1444   - ret = load_elf(buf, hwdef->slavio_base - PROM_VADDR, NULL, NULL, NULL);
1445   - if (ret < 0 || ret > PROM_SIZE_MAX)
1446   - ret = load_image_targphys(buf, hwdef->slavio_base, PROM_SIZE_MAX);
  1457 + filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  1458 + if (filename) {
  1459 + ret = load_elf(filename, hwdef->slavio_base - PROM_VADDR,
  1460 + NULL, NULL, NULL);
  1461 + if (ret < 0 || ret > PROM_SIZE_MAX)
  1462 + ret = load_image_targphys(filename, hwdef->slavio_base,
  1463 + PROM_SIZE_MAX);
  1464 + qemu_free(filename);
  1465 + } else {
  1466 + ret = -1;
  1467 + }
1447 1468 if (ret < 0 || ret > PROM_SIZE_MAX) {
1448 1469 fprintf(stderr, "qemu: could not load prom '%s'\n",
1449   - buf);
  1470 + filename);
1450 1471 exit(1);
1451 1472 }
1452 1473  
... ...
hw/sun4u.c
... ... @@ -333,7 +333,7 @@ static void sun4uv_init(ram_addr_t RAM_size,
333 333 const struct hwdef *hwdef)
334 334 {
335 335 CPUState *env;
336   - char buf[1024];
  336 + char *filename;
337 337 m48t59_t *nvram;
338 338 int ret, linux_boot;
339 339 unsigned int i;
... ... @@ -392,17 +392,23 @@ static void sun4uv_init(ram_addr_t RAM_size,
392 392  
393 393 if (bios_name == NULL)
394 394 bios_name = PROM_FILENAME;
395   - snprintf(buf, sizeof(buf), "%s/%s", bios_dir, bios_name);
396   - ret = load_elf(buf, hwdef->prom_addr - PROM_VADDR, NULL, NULL, NULL);
397   - if (ret < 0) {
398   - ret = load_image_targphys(buf, hwdef->prom_addr,
399   - (PROM_SIZE_MAX + TARGET_PAGE_SIZE) &
400   - TARGET_PAGE_MASK);
  395 + filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, bios_name);
  396 + if (filename) {
  397 + ret = load_elf(filename, hwdef->prom_addr - PROM_VADDR,
  398 + NULL, NULL, NULL);
401 399 if (ret < 0) {
402   - fprintf(stderr, "qemu: could not load prom '%s'\n",
403   - buf);
404   - exit(1);
  400 + ret = load_image_targphys(filename, hwdef->prom_addr,
  401 + (PROM_SIZE_MAX + TARGET_PAGE_SIZE) &
  402 + TARGET_PAGE_MASK);
405 403 }
  404 + qemu_free(filename);
  405 + } else {
  406 + ret = -1;
  407 + }
  408 + if (ret < 0) {
  409 + fprintf(stderr, "qemu: could not load prom '%s'\n",
  410 + bios_name);
  411 + exit(1);
406 412 }
407 413  
408 414 kernel_size = 0;
... ...
keymaps.c
... ... @@ -64,20 +64,20 @@ static kbd_layout_t *parse_keyboard_layout(const name2keysym_t *table,
64 64 kbd_layout_t * k)
65 65 {
66 66 FILE *f;
67   - char file_name[1024];
  67 + char * filename;
68 68 char line[1024];
69 69 int len;
70 70  
71   - snprintf(file_name, sizeof(file_name),
72   - "%s/keymaps/%s", bios_dir, language);
  71 + filename = qemu_find_file(QEMU_FILE_TYPE_KEYMAP, language);
73 72  
74 73 if (!k)
75 74 k = qemu_mallocz(sizeof(kbd_layout_t));
76   - if (!(f = fopen(file_name, "r"))) {
  75 + if (!(filename && (f = fopen(filename, "r")))) {
77 76 fprintf(stderr,
78   - "Could not read keymap file: '%s'\n", file_name);
  77 + "Could not read keymap file: '%s'\n", language);
79 78 return 0;
80 79 }
  80 + qemu_free(filename);
81 81 for(;;) {
82 82 if (fgets(line, 1024, f) == NULL)
83 83 break;
... ...
sysemu.h
... ... @@ -10,7 +10,10 @@
10 10  
11 11 /* vl.c */
12 12 extern const char *bios_name;
13   -extern const char *bios_dir;
  13 +
  14 +#define QEMU_FILE_TYPE_BIOS 0
  15 +#define QEMU_FILE_TYPE_KEYMAP 1
  16 +char *qemu_find_file(int type, const char *name);
14 17  
15 18 extern int vm_running;
16 19 extern const char *qemu_name;
... ...
... ... @@ -33,6 +33,7 @@
33 33 #include "config-host.h"
34 34  
35 35 #ifndef _WIN32
  36 +#include <libgen.h>
36 37 #include <pwd.h>
37 38 #include <sys/times.h>
38 39 #include <sys/wait.h>
... ... @@ -191,7 +192,7 @@ int main(int argc, char **argv)
191 192 /* XXX: use a two level table to limit memory usage */
192 193 #define MAX_IOPORTS 65536
193 194  
194   -const char *bios_dir = CONFIG_QEMU_SHAREDIR;
  195 +static const char *data_dir;
195 196 const char *bios_name = NULL;
196 197 static void *ioport_opaque[MAX_IOPORTS];
197 198 static IOPortReadFunc *ioport_read_table[3][MAX_IOPORTS];
... ... @@ -4795,6 +4796,128 @@ static void termsig_setup(void)
4795 4796  
4796 4797 #endif
4797 4798  
  4799 +#ifdef _WIN32
  4800 +/* Look for support files in the same directory as the executable. */
  4801 +static char *find_datadir(const char *argv0)
  4802 +{
  4803 + char *p;
  4804 + char buf[MAX_PATH];
  4805 + DWORD len;
  4806 +
  4807 + len = GetModuleFileName(NULL, buf, sizeof(buf) - 1);
  4808 + if (len == 0) {
  4809 + return len;
  4810 + }
  4811 +
  4812 + buf[len] = 0;
  4813 + p = buf + len - 1;
  4814 + while (p != buf && *p != '\\')
  4815 + p--;
  4816 + *p = 0;
  4817 + if (access(buf, R_OK) == 0) {
  4818 + return qemu_strdup(buf);
  4819 + }
  4820 + return NULL;
  4821 +}
  4822 +#else /* !_WIN32 */
  4823 +
  4824 +/* Find a likely location for support files using the location of the binary.
  4825 + For installed binaries this will be "$bindir/../share/qemu". When
  4826 + running from the build tree this will be "$bindir/../pc-bios". */
  4827 +#define SHARE_SUFFIX "/share/qemu"
  4828 +#define BUILD_SUFFIX "/pc-bios"
  4829 +static char *find_datadir(const char *argv0)
  4830 +{
  4831 + char *dir;
  4832 + char *p = NULL;
  4833 + char *res;
  4834 +#ifdef PATH_MAX
  4835 + char buf[PATH_MAX];
  4836 +#endif
  4837 +
  4838 +#if defined(__linux__)
  4839 + {
  4840 + int len;
  4841 + len = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
  4842 + if (len > 0) {
  4843 + buf[len] = 0;
  4844 + p = buf;
  4845 + }
  4846 + }
  4847 +#elif defined(__FreeBSD__)
  4848 + {
  4849 + int len;
  4850 + len = readlink("/proc/curproc/file", buf, sizeof(buf) - 1);
  4851 + if (len > 0) {
  4852 + buf[len] = 0;
  4853 + p = buf;
  4854 + }
  4855 + }
  4856 +#endif
  4857 + /* If we don't have any way of figuring out the actual executable
  4858 + location then try argv[0]. */
  4859 + if (!p) {
  4860 +#ifdef PATH_MAX
  4861 + p = buf;
  4862 +#endif
  4863 + p = realpath(argv0, p);
  4864 + if (!p) {
  4865 + return NULL;
  4866 + }
  4867 + }
  4868 + dir = dirname(p);
  4869 + dir = dirname(dir);
  4870 +
  4871 + res = qemu_mallocz(strlen(dir) +
  4872 + MAX(strlen(SHARE_SUFFIX), strlen(BUILD_SUFFIX)) + 1);
  4873 + sprintf(res, "%s%s", dir, SHARE_SUFFIX);
  4874 + if (access(res, R_OK)) {
  4875 + sprintf(res, "%s%s", dir, BUILD_SUFFIX);
  4876 + if (access(res, R_OK)) {
  4877 + qemu_free(res);
  4878 + res = NULL;
  4879 + }
  4880 + }
  4881 +#ifndef PATH_MAX
  4882 + free(p);
  4883 +#endif
  4884 + return res;
  4885 +}
  4886 +#undef SHARE_SUFFIX
  4887 +#undef BUILD_SUFFIX
  4888 +#endif
  4889 +
  4890 +char *qemu_find_file(int type, const char *name)
  4891 +{
  4892 + int len;
  4893 + const char *subdir;
  4894 + char *buf;
  4895 +
  4896 + /* If name contains path separators then try it as a straight path. */
  4897 + if ((strchr(name, '/') || strchr(name, '\\'))
  4898 + && access(name, R_OK) == 0) {
  4899 + return strdup(name);
  4900 + }
  4901 + switch (type) {
  4902 + case QEMU_FILE_TYPE_BIOS:
  4903 + subdir = "";
  4904 + break;
  4905 + case QEMU_FILE_TYPE_KEYMAP:
  4906 + subdir = "keymaps/";
  4907 + break;
  4908 + default:
  4909 + abort();
  4910 + }
  4911 + len = strlen(data_dir) + strlen(name) + strlen(subdir) + 2;
  4912 + buf = qemu_mallocz(len);
  4913 + sprintf(buf, "%s/%s%s", data_dir, subdir, name);
  4914 + if (access(buf, R_OK)) {
  4915 + qemu_free(buf);
  4916 + return NULL;
  4917 + }
  4918 + return buf;
  4919 +}
  4920 +
4798 4921 int main(int argc, char **argv, char **envp)
4799 4922 {
4800 4923 const char *gdbstub_dev = NULL;
... ... @@ -5234,7 +5357,7 @@ int main(int argc, char **argv, char **envp)
5234 5357 gdbstub_dev = optarg;
5235 5358 break;
5236 5359 case QEMU_OPTION_L:
5237   - bios_dir = optarg;
  5360 + data_dir = optarg;
5238 5361 break;
5239 5362 case QEMU_OPTION_bios:
5240 5363 bios_name = optarg;
... ... @@ -5560,6 +5683,16 @@ int main(int argc, char **argv, char **envp)
5560 5683 }
5561 5684 }
5562 5685  
  5686 + /* If no data_dir is specified then try to find it relative to the
  5687 + executable path. */
  5688 + if (!data_dir) {
  5689 + data_dir = find_datadir(argv[0]);
  5690 + }
  5691 + /* If all else fails use the install patch specified when building. */
  5692 + if (!data_dir) {
  5693 + data_dir = CONFIG_QEMU_SHAREDIR;
  5694 + }
  5695 +
5563 5696 #if defined(CONFIG_KVM) && defined(CONFIG_KQEMU)
5564 5697 if (kvm_allowed && kqemu_allowed) {
5565 5698 fprintf(stderr,
... ... @@ -5705,19 +5838,24 @@ int main(int argc, char **argv, char **envp)
5705 5838 for (i = 0; i < nb_nics && i < 4; i++) {
5706 5839 const char *model = nd_table[i].model;
5707 5840 char buf[1024];
  5841 + char *filename;
5708 5842 if (net_boot & (1 << i)) {
5709 5843 if (model == NULL)
5710 5844 model = "ne2k_pci";
5711   - snprintf(buf, sizeof(buf), "%s/pxe-%s.bin", bios_dir, model);
5712   - if (get_image_size(buf) > 0) {
  5845 + snprintf(buf, sizeof(buf), "pxe-%s.bin", model);
  5846 + filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, buf);
  5847 + if (filename && get_image_size(filename) > 0) {
5713 5848 if (nb_option_roms >= MAX_OPTION_ROMS) {
5714 5849 fprintf(stderr, "Too many option ROMs\n");
5715 5850 exit(1);
5716 5851 }
5717   - option_rom[nb_option_roms] = strdup(buf);
  5852 + option_rom[nb_option_roms] = qemu_strdup(buf);
5718 5853 nb_option_roms++;
5719 5854 netroms++;
5720 5855 }
  5856 + if (filename) {
  5857 + qemu_free(filename);
  5858 + }
5721 5859 }
5722 5860 }
5723 5861 if (netroms == 0) {
... ...