xref: /openbmc/qemu/hw/avr/boot.c (revision 1be5a765c08cee3a9587c8a8d3fc2ea247b13f9c)
17dd8f6fdSPhilippe Mathieu-Daudé /*
27dd8f6fdSPhilippe Mathieu-Daudé  * AVR loader helpers
37dd8f6fdSPhilippe Mathieu-Daudé  *
47dd8f6fdSPhilippe Mathieu-Daudé  * Copyright (c) 2019-2020 Philippe Mathieu-Daudé
57dd8f6fdSPhilippe Mathieu-Daudé  *
67dd8f6fdSPhilippe Mathieu-Daudé  * This work is licensed under the terms of the GNU GPLv2 or later.
77dd8f6fdSPhilippe Mathieu-Daudé  * See the COPYING file in the top-level directory.
87dd8f6fdSPhilippe Mathieu-Daudé  * SPDX-License-Identifier: GPL-2.0-or-later
97dd8f6fdSPhilippe Mathieu-Daudé  */
107dd8f6fdSPhilippe Mathieu-Daudé 
117dd8f6fdSPhilippe Mathieu-Daudé #include "qemu/osdep.h"
12*2c65db5eSPaolo Bonzini #include "qemu/datadir.h"
137dd8f6fdSPhilippe Mathieu-Daudé #include "hw/loader.h"
147dd8f6fdSPhilippe Mathieu-Daudé #include "elf.h"
157dd8f6fdSPhilippe Mathieu-Daudé #include "boot.h"
167dd8f6fdSPhilippe Mathieu-Daudé #include "qemu/error-report.h"
177dd8f6fdSPhilippe Mathieu-Daudé 
avr_elf_e_flags_to_cpu_type(uint32_t flags)187dd8f6fdSPhilippe Mathieu-Daudé static const char *avr_elf_e_flags_to_cpu_type(uint32_t flags)
197dd8f6fdSPhilippe Mathieu-Daudé {
207dd8f6fdSPhilippe Mathieu-Daudé     switch (flags & EF_AVR_MACH) {
217dd8f6fdSPhilippe Mathieu-Daudé     case bfd_mach_avr1:
227dd8f6fdSPhilippe Mathieu-Daudé         return AVR_CPU_TYPE_NAME("avr1");
237dd8f6fdSPhilippe Mathieu-Daudé     case bfd_mach_avr2:
247dd8f6fdSPhilippe Mathieu-Daudé         return AVR_CPU_TYPE_NAME("avr2");
257dd8f6fdSPhilippe Mathieu-Daudé     case bfd_mach_avr25:
267dd8f6fdSPhilippe Mathieu-Daudé         return AVR_CPU_TYPE_NAME("avr25");
277dd8f6fdSPhilippe Mathieu-Daudé     case bfd_mach_avr3:
287dd8f6fdSPhilippe Mathieu-Daudé         return AVR_CPU_TYPE_NAME("avr3");
297dd8f6fdSPhilippe Mathieu-Daudé     case bfd_mach_avr31:
307dd8f6fdSPhilippe Mathieu-Daudé         return AVR_CPU_TYPE_NAME("avr31");
317dd8f6fdSPhilippe Mathieu-Daudé     case bfd_mach_avr35:
327dd8f6fdSPhilippe Mathieu-Daudé         return AVR_CPU_TYPE_NAME("avr35");
337dd8f6fdSPhilippe Mathieu-Daudé     case bfd_mach_avr4:
347dd8f6fdSPhilippe Mathieu-Daudé         return AVR_CPU_TYPE_NAME("avr4");
357dd8f6fdSPhilippe Mathieu-Daudé     case bfd_mach_avr5:
367dd8f6fdSPhilippe Mathieu-Daudé         return AVR_CPU_TYPE_NAME("avr5");
377dd8f6fdSPhilippe Mathieu-Daudé     case bfd_mach_avr51:
387dd8f6fdSPhilippe Mathieu-Daudé         return AVR_CPU_TYPE_NAME("avr51");
397dd8f6fdSPhilippe Mathieu-Daudé     case bfd_mach_avr6:
407dd8f6fdSPhilippe Mathieu-Daudé         return AVR_CPU_TYPE_NAME("avr6");
417dd8f6fdSPhilippe Mathieu-Daudé     case bfd_mach_avrtiny:
427dd8f6fdSPhilippe Mathieu-Daudé         return AVR_CPU_TYPE_NAME("avrtiny");
437dd8f6fdSPhilippe Mathieu-Daudé     case bfd_mach_avrxmega2:
447dd8f6fdSPhilippe Mathieu-Daudé         return AVR_CPU_TYPE_NAME("xmega2");
457dd8f6fdSPhilippe Mathieu-Daudé     case bfd_mach_avrxmega3:
467dd8f6fdSPhilippe Mathieu-Daudé         return AVR_CPU_TYPE_NAME("xmega3");
477dd8f6fdSPhilippe Mathieu-Daudé     case bfd_mach_avrxmega4:
487dd8f6fdSPhilippe Mathieu-Daudé         return AVR_CPU_TYPE_NAME("xmega4");
497dd8f6fdSPhilippe Mathieu-Daudé     case bfd_mach_avrxmega5:
507dd8f6fdSPhilippe Mathieu-Daudé         return AVR_CPU_TYPE_NAME("xmega5");
517dd8f6fdSPhilippe Mathieu-Daudé     case bfd_mach_avrxmega6:
527dd8f6fdSPhilippe Mathieu-Daudé         return AVR_CPU_TYPE_NAME("xmega6");
537dd8f6fdSPhilippe Mathieu-Daudé     case bfd_mach_avrxmega7:
547dd8f6fdSPhilippe Mathieu-Daudé         return AVR_CPU_TYPE_NAME("xmega7");
557dd8f6fdSPhilippe Mathieu-Daudé     default:
567dd8f6fdSPhilippe Mathieu-Daudé         return NULL;
577dd8f6fdSPhilippe Mathieu-Daudé     }
587dd8f6fdSPhilippe Mathieu-Daudé }
597dd8f6fdSPhilippe Mathieu-Daudé 
avr_load_firmware(AVRCPU * cpu,MachineState * ms,MemoryRegion * program_mr,const char * firmware)607dd8f6fdSPhilippe Mathieu-Daudé bool avr_load_firmware(AVRCPU *cpu, MachineState *ms,
617dd8f6fdSPhilippe Mathieu-Daudé                        MemoryRegion *program_mr, const char *firmware)
627dd8f6fdSPhilippe Mathieu-Daudé {
635e29521aSPhilippe Mathieu-Daudé     g_autofree char *filename = NULL;
647dd8f6fdSPhilippe Mathieu-Daudé     int bytes_loaded;
657dd8f6fdSPhilippe Mathieu-Daudé     uint64_t entry;
667dd8f6fdSPhilippe Mathieu-Daudé     uint32_t e_flags;
677dd8f6fdSPhilippe Mathieu-Daudé 
687dd8f6fdSPhilippe Mathieu-Daudé     filename = qemu_find_file(QEMU_FILE_TYPE_BIOS, firmware);
697dd8f6fdSPhilippe Mathieu-Daudé     if (filename == NULL) {
707dd8f6fdSPhilippe Mathieu-Daudé         error_report("Unable to find %s", firmware);
717dd8f6fdSPhilippe Mathieu-Daudé         return false;
727dd8f6fdSPhilippe Mathieu-Daudé     }
737dd8f6fdSPhilippe Mathieu-Daudé 
747dd8f6fdSPhilippe Mathieu-Daudé     bytes_loaded = load_elf_ram_sym(filename,
757dd8f6fdSPhilippe Mathieu-Daudé                                     NULL, NULL, NULL,
767dd8f6fdSPhilippe Mathieu-Daudé                                     &entry, NULL, NULL,
777dd8f6fdSPhilippe Mathieu-Daudé                                     &e_flags, 0, EM_AVR, 0, 0,
787dd8f6fdSPhilippe Mathieu-Daudé                                     NULL, true, NULL);
797dd8f6fdSPhilippe Mathieu-Daudé     if (bytes_loaded >= 0) {
807dd8f6fdSPhilippe Mathieu-Daudé         /* If ELF file is provided, determine CPU type reading ELF e_flags. */
817dd8f6fdSPhilippe Mathieu-Daudé         const char *elf_cpu = avr_elf_e_flags_to_cpu_type(e_flags);
827dd8f6fdSPhilippe Mathieu-Daudé         const char *mcu_cpu_type = object_get_typename(OBJECT(cpu));
837dd8f6fdSPhilippe Mathieu-Daudé         int cpu_len = strlen(mcu_cpu_type) - strlen(AVR_CPU_TYPE_SUFFIX);
847dd8f6fdSPhilippe Mathieu-Daudé 
857dd8f6fdSPhilippe Mathieu-Daudé         if (entry) {
867dd8f6fdSPhilippe Mathieu-Daudé             error_report("BIOS entry_point must be 0x0000 "
877dd8f6fdSPhilippe Mathieu-Daudé                          "(ELF image '%s' has entry_point 0x%04" PRIx64 ")",
887dd8f6fdSPhilippe Mathieu-Daudé                          firmware, entry);
897dd8f6fdSPhilippe Mathieu-Daudé             return false;
907dd8f6fdSPhilippe Mathieu-Daudé         }
917dd8f6fdSPhilippe Mathieu-Daudé         if (!elf_cpu) {
927dd8f6fdSPhilippe Mathieu-Daudé             warn_report("Could not determine CPU type for ELF image '%s', "
937dd8f6fdSPhilippe Mathieu-Daudé                         "assuming '%.*s' CPU",
947dd8f6fdSPhilippe Mathieu-Daudé                          firmware, cpu_len, mcu_cpu_type);
957dd8f6fdSPhilippe Mathieu-Daudé             return true;
967dd8f6fdSPhilippe Mathieu-Daudé         }
977dd8f6fdSPhilippe Mathieu-Daudé         if (strcmp(elf_cpu, mcu_cpu_type)) {
987dd8f6fdSPhilippe Mathieu-Daudé             error_report("Current machine: %s with '%.*s' CPU",
997dd8f6fdSPhilippe Mathieu-Daudé                          MACHINE_GET_CLASS(ms)->desc, cpu_len, mcu_cpu_type);
1007dd8f6fdSPhilippe Mathieu-Daudé             error_report("ELF image '%s' is for '%.*s' CPU",
1017dd8f6fdSPhilippe Mathieu-Daudé                          firmware,
1027dd8f6fdSPhilippe Mathieu-Daudé                          (int)(strlen(elf_cpu) - strlen(AVR_CPU_TYPE_SUFFIX)),
1037dd8f6fdSPhilippe Mathieu-Daudé                          elf_cpu);
1047dd8f6fdSPhilippe Mathieu-Daudé             return false;
1057dd8f6fdSPhilippe Mathieu-Daudé         }
1067dd8f6fdSPhilippe Mathieu-Daudé     } else {
1077dd8f6fdSPhilippe Mathieu-Daudé         bytes_loaded = load_image_mr(filename, program_mr);
1087dd8f6fdSPhilippe Mathieu-Daudé     }
1097dd8f6fdSPhilippe Mathieu-Daudé     if (bytes_loaded < 0) {
1107dd8f6fdSPhilippe Mathieu-Daudé         error_report("Unable to load firmware image %s as ELF or raw binary",
1117dd8f6fdSPhilippe Mathieu-Daudé                      firmware);
1127dd8f6fdSPhilippe Mathieu-Daudé         return false;
1137dd8f6fdSPhilippe Mathieu-Daudé     }
1147dd8f6fdSPhilippe Mathieu-Daudé     return true;
1157dd8f6fdSPhilippe Mathieu-Daudé }
116