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