From a258a14bf05a144599f6a41cb234efcc8b81a448 Mon Sep 17 00:00:00 2001 From: will-v-pi <108662275+will-v-pi@users.noreply.github.com> Date: Wed, 4 Dec 2024 12:39:15 +0000 Subject: [PATCH] Add verbose output from picotool uf2 convert (#186) Pass the settings.verbose setting to elf2uf2.cpp functions, to add verbose output to picotool uf2 convert Fixes #185 --- elf2uf2/elf2uf2.cpp | 29 ++++++++++++++++------------- elf2uf2/elf2uf2.h | 4 ++-- main.cpp | 4 ++-- 3 files changed, 20 insertions(+), 17 deletions(-) diff --git a/elf2uf2/elf2uf2.cpp b/elf2uf2/elf2uf2.cpp index 63ce372..eb8e094 100644 --- a/elf2uf2/elf2uf2.cpp +++ b/elf2uf2/elf2uf2.cpp @@ -19,7 +19,7 @@ #define FLASH_SECTOR_ERASE_SIZE 4096u -static bool verbose; +static bool g_verbose; static void fail_read_error() { fail(ERROR_READ_FAILED, "Failed to read input file"); @@ -43,7 +43,7 @@ int check_address_range(const address_ranges& valid_ranges, uint32_t addr, uint3 fail(ERROR_INCOMPATIBLE, "ELF contains memory contents for uninitialized memory at %p", addr); } ar = range; - if (verbose) { + if (g_verbose) { printf("%s segment %08x->%08x (%08x->%08x)\n", uninitialized ? "Uninitialized" : "Mapped", addr, addr + size, vaddr, vaddr+size); } @@ -65,7 +65,7 @@ int check_elf32_ph_entries(const std::vector& entries, const add if (rc) return rc; // we don't download uninitialized, generally it is BSS and should be zero-ed by crt0.S, or it may be COPY areas which are undefined if (ar.type != address_range::type::CONTENTS) { - if (verbose) printf(" ignored\n"); + if (g_verbose) printf(" ignored\n"); continue; } unsigned int addr = entry.paddr; @@ -180,7 +180,7 @@ int pages2uf2(std::map>& pages, std::shared for(auto& page_entry : pages) { block.target_addr = page_entry.first; block.block_no = page_num++; - if (verbose) { + if (g_verbose) { printf("Page %d / %d %08x%s\n", block.block_no, block.num_blocks, block.target_addr, page_entry.second.empty() ? " (padding)": ""); } @@ -195,7 +195,8 @@ int pages2uf2(std::map>& pages, std::shared return 0; } -int bin2uf2(std::shared_ptr in, std::shared_ptr out, uint32_t address, uint32_t family_id, uint32_t abs_block_loc) { +int bin2uf2(std::shared_ptr in, std::shared_ptr out, uint32_t address, uint32_t family_id, uint32_t abs_block_loc, bool verbose) { + g_verbose = verbose; std::map> pages; in->seekg(0, in->end); @@ -233,11 +234,13 @@ int bin2uf2(std::shared_ptr in, std::shared_ptr ou return pages2uf2(pages, in, out, family_id, abs_block_loc); } -int elf2uf2(std::shared_ptr in, std::shared_ptr out, uint32_t family_id, uint32_t package_addr, uint32_t abs_block_loc) { - elf_file elf; +int elf2uf2(std::shared_ptr in, std::shared_ptr out, uint32_t family_id, uint32_t package_addr, uint32_t abs_block_loc, bool verbose) { + elf_file source_file(verbose); + g_verbose = verbose; + elf_file *elf = &source_file; std::map> pages; - int rc = elf.read_file(in); + int rc = elf->read_file(in); bool ram_style = false; address_ranges valid_ranges = {}; address_ranges flash_range; address_ranges ram_range; @@ -249,9 +252,9 @@ int elf2uf2(std::shared_ptr in, std::shared_ptr ou ram_range = rp2350_address_ranges_ram; } if (!rc) { - rc = rp_determine_binary_type(elf.header(), elf.segments(), flash_range, ram_range, &ram_style); + rc = rp_determine_binary_type(elf->header(), elf->segments(), flash_range, ram_range, &ram_style); if (!rc) { - if (verbose) { + if (g_verbose) { if (ram_style) { printf("Detected RAM binary\n"); } else { @@ -259,7 +262,7 @@ int elf2uf2(std::shared_ptr in, std::shared_ptr ou } } valid_ranges = ram_style ? ram_range : flash_range; - rc = check_elf32_ph_entries(elf.segments(), valid_ranges, pages); + rc = check_elf32_ph_entries(elf->segments(), valid_ranges, pages); } } if (rc) return rc; @@ -267,7 +270,7 @@ int elf2uf2(std::shared_ptr in, std::shared_ptr ou fail(ERROR_INCOMPATIBLE, "The input file has no memory pages"); } // No Thumb bit on RISC-V - elf32_header eh = elf.header(); + elf32_header eh = elf->header(); uint32_t thumb_bit = eh.common.machine == EM_ARM ? 0x1u : 0x0u; if (ram_style) { uint32_t expected_ep_main_ram = UINT32_MAX; @@ -331,7 +334,7 @@ int elf2uf2(std::shared_ptr in, std::shared_ptr ou // Package binary at address uint32_t base_addr = pages.begin()->first; int32_t package_delta = package_addr - base_addr; - if (verbose) printf("Base %x\n", base_addr); + if (g_verbose) printf("Base %x\n", base_addr); auto copy_pages = pages; pages.clear(); diff --git a/elf2uf2/elf2uf2.h b/elf2uf2/elf2uf2.h index 2478373..02ae6cd 100644 --- a/elf2uf2/elf2uf2.h +++ b/elf2uf2/elf2uf2.h @@ -21,8 +21,8 @@ bool check_abs_block(uf2_block block); -int bin2uf2(std::shared_ptr in, std::shared_ptr out, uint32_t address, uint32_t family_id, uint32_t abs_block_loc=0); -int elf2uf2(std::shared_ptr in, std::shared_ptr out, uint32_t family_id, uint32_t package_addr=0, uint32_t abs_block_loc=0); +int bin2uf2(std::shared_ptr in, std::shared_ptr out, uint32_t address, uint32_t family_id, uint32_t abs_block_loc=0, bool verbose=false); +int elf2uf2(std::shared_ptr in, std::shared_ptr out, uint32_t family_id, uint32_t package_addr=0, uint32_t abs_block_loc=0, bool verbose=false); #endif \ No newline at end of file diff --git a/main.cpp b/main.cpp index 1f75cd3..f473e4e 100644 --- a/main.cpp +++ b/main.cpp @@ -6180,10 +6180,10 @@ bool uf2_convert_command::execute(device_map &devices) { #endif if (get_file_type() == filetype::elf) { uint32_t package_address = settings.offset_set ? settings.offset : 0; - elf2uf2(in, out, family_id, package_address, settings.uf2.abs_block_loc); + elf2uf2(in, out, family_id, package_address, settings.uf2.abs_block_loc, settings.verbose); } else if (get_file_type() == filetype::bin) { uint32_t address = settings.offset_set ? settings.offset : FLASH_START; - bin2uf2(in, out, address, family_id, settings.uf2.abs_block_loc); + bin2uf2(in, out, address, family_id, settings.uf2.abs_block_loc, settings.verbose); } else { fail(ERROR_ARGS, "Convert currently only from ELF/BIN to UF2\n"); }