/* * VAS_EBOOT -- GRand Unified Bootloader * Copyright (C) 2013-2015 Free Software Foundation, Inc. * Copyright (C) 2024 Canonical, Ltd. * * VAS_EBOOT is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * VAS_EBOOT is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with VAS_EBOOT. If not, see . */ #include #include #include #include #include #include #include #include #include #include #include #include static void *loaded_fdt; static void *fdt; #define FDT_ADDR_CELLS_STRING "#address-cells" #define FDT_SIZE_CELLS_STRING "#size-cells" #define FDT_ADDR_SIZE_EXTRA ((2 * VasEBoot_fdt_prop_entry_size (sizeof(VasEBoot_uint32_t))) + \ sizeof (FDT_ADDR_CELLS_STRING) + \ sizeof (FDT_SIZE_CELLS_STRING)) static const struct VasEBoot_arg_option options_fdtdump[] = { {"prop", 'p', 0, N_("Get property."), N_("prop"), ARG_TYPE_STRING}, {"set", '\0', 0, N_("Store the value in the given variable name."), N_("variable"), ARG_TYPE_STRING}, {0, 0, 0, 0, 0, 0} }; void * VasEBoot_fdt_load (VasEBoot_size_t additional_size) { void *raw_fdt; unsigned int size; if (fdt) { size = VAS_EBOOT_EFI_BYTES_TO_PAGES (VasEBoot_fdt_get_totalsize (fdt)); VasEBoot_efi_free_pages ((VasEBoot_addr_t) fdt, size); } if (loaded_fdt) raw_fdt = loaded_fdt; else raw_fdt = VasEBoot_efi_get_firmware_fdt(); if (raw_fdt) size = VasEBoot_fdt_get_totalsize (raw_fdt); else size = VAS_EBOOT_FDT_EMPTY_TREE_SZ + FDT_ADDR_SIZE_EXTRA; size += additional_size; VasEBoot_dprintf ("linux", "allocating %d bytes for fdt\n", size); fdt = VasEBoot_efi_allocate_pages_real (VAS_EBOOT_EFI_MAX_USABLE_ADDRESS, VAS_EBOOT_EFI_BYTES_TO_PAGES (size), VAS_EBOOT_EFI_ALLOCATE_MAX_ADDRESS, VAS_EBOOT_EFI_ACPI_RECLAIM_MEMORY); if (!fdt) return NULL; if (raw_fdt) { VasEBoot_memmove (fdt, raw_fdt, size - additional_size); VasEBoot_fdt_set_totalsize (fdt, size); } else { VasEBoot_fdt_create_empty_tree (fdt, size); VasEBoot_fdt_set_prop32 (fdt, 0, FDT_ADDR_CELLS_STRING, 2); VasEBoot_fdt_set_prop32 (fdt, 0, FDT_SIZE_CELLS_STRING, 2); } return fdt; } VasEBoot_err_t VasEBoot_fdt_install (void) { VasEBoot_efi_boot_services_t *b; static VasEBoot_guid_t fdt_guid = VAS_EBOOT_EFI_DEVICE_TREE_GUID; VasEBoot_efi_status_t status; if (fdt == NULL && loaded_fdt == NULL) return VAS_EBOOT_ERR_NONE; b = VasEBoot_efi_system_table->boot_services; status = b->install_configuration_table (&fdt_guid, fdt ? fdt : loaded_fdt); if (status != VAS_EBOOT_EFI_SUCCESS) return VasEBoot_error (VAS_EBOOT_ERR_IO, "failed to install FDT"); VasEBoot_dprintf ("fdt", "Installed/updated FDT configuration table @ %p\n", fdt ? fdt : loaded_fdt); return VAS_EBOOT_ERR_NONE; } void VasEBoot_fdt_unload (void) { if (!fdt) { return; } VasEBoot_efi_free_pages ((VasEBoot_addr_t) fdt, VAS_EBOOT_EFI_BYTES_TO_PAGES (VasEBoot_fdt_get_totalsize (fdt))); fdt = NULL; } static VasEBoot_err_t VasEBoot_cmd_devicetree (VasEBoot_command_t cmd __attribute__ ((unused)), int argc, char *argv[]) { VasEBoot_file_t dtb; void *blob = NULL; int size; if (loaded_fdt) VasEBoot_free (loaded_fdt); loaded_fdt = NULL; /* No arguments means "use firmware FDT". */ if (argc == 0) { return VAS_EBOOT_ERR_NONE; } dtb = VasEBoot_file_open (argv[0], VAS_EBOOT_FILE_TYPE_DEVICE_TREE_IMAGE); if (!dtb) goto out; size = VasEBoot_file_size (dtb); blob = VasEBoot_malloc (size); if (!blob) goto out; if (VasEBoot_file_read (dtb, blob, size) < size) { if (!VasEBoot_errno) VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, N_("premature end of file %s"), argv[0]); goto out; } if (VasEBoot_fdt_check_header (blob, size) != 0) { VasEBoot_error (VAS_EBOOT_ERR_BAD_OS, N_("invalid device tree")); goto out; } out: if (dtb) VasEBoot_file_close (dtb); if (blob) { if (VasEBoot_errno == VAS_EBOOT_ERR_NONE) loaded_fdt = blob; else VasEBoot_free (blob); } return VasEBoot_errno; } static VasEBoot_err_t VasEBoot_cmd_fdtdump (VasEBoot_extcmd_context_t ctxt, int argc __attribute__ ((unused)), char **argv __attribute__ ((unused))) { struct VasEBoot_arg_list *state = ctxt->state; const char *value = NULL; void *fw_fdt; fw_fdt = VasEBoot_efi_get_firmware_fdt (); if (fw_fdt == NULL) return VasEBoot_error (VAS_EBOOT_ERR_IO, N_("No device tree found")); if (state[0].set) value = VasEBoot_fdt_get_prop (fw_fdt, 0, state[0].arg, NULL); if (value == NULL) return VasEBoot_error (VAS_EBOOT_ERR_OUT_OF_RANGE, N_("failed to retrieve the prop field")); if (state[1].set) VasEBoot_env_set (state[1].arg, value); else VasEBoot_printf ("%s\n", value); return VAS_EBOOT_ERR_NONE; } static VasEBoot_command_t cmd_devicetree; static VasEBoot_extcmd_t cmd_fdtdump; VAS_EBOOT_MOD_INIT (fdt) { cmd_fdtdump = VasEBoot_register_extcmd ("fdtdump", VasEBoot_cmd_fdtdump, 0, N_("[-p] [--set variable]"), N_("Retrieve device tree information."), options_fdtdump); cmd_devicetree = VasEBoot_register_command_lockdown ("devicetree", VasEBoot_cmd_devicetree, 0, N_("Load DTB file.")); } VAS_EBOOT_MOD_FINI (fdt) { VasEBoot_unregister_command (cmd_devicetree); VasEBoot_unregister_extcmd (cmd_fdtdump); }