elf: add better bootm support
v6 cover elf: add better bootm support
Currently, when booting an elf file using "bootm /dev/mtdx", bootm will simply pass the file to the bootm handler and the read done on it will read the entire flash partition. This series starts by some cleanup and then modify the elf loader to load the elf file without copying the whole elf file to an intermediate buffer. Only the elf header is copied in order to parse the elf file into a list of segment and then, the segments are loaded directly from the file.
A special handling for the elf file is also added in bootm data to allow using directly the elf file structure. Finally the mips bootm handler is modified to use bootm_load_os directly instead of manual elf loading.
Compilation for both mips and arm has been tested but run on qemu-malta was not possible. Changes have been tested on kvx architecture for which bootm support has been added and will be submitted.
Changes v5 -> v6
- Add missing elf boundaries computation patch
Changes v4 -> v5
- Rework elf loading to load from file rather than a buffer
- Use calloc instead of xzalloc in elf file loading
- Add check of program headers in elf check
Changes v3 -> v4
- Fix init of elf entry address to be used by bootm_load_elf
Changes v2 -> v3
- Integrate elf loading in bootm_load_os
- Add patch to remove now unused elf_load_image/elf_release_image
- Use malloc instead of xmalloc and check return value
Changes v1 -> v2
- Add BOOTM_ELF config to select elf support and add checks in code
- Add an elf_get_mem_size function to avoid computing elf size in bootm.c
- Use xmalloc and read_full in elf_open instead of xzalloc/read
- Fix data->elf NULL reset
- Remove elf struct entirely from mips bootm code
Clement Leger (8):
common: elf: add computation of elf boundaries
common: elf: fix warning on 32 bits architectures
common: elf: use calloc instead of xzalloc
common: elf: check number of elf program headers
common: elf: load elf directly from file
common: elf: add elf_open, elf_close and elf_load
common: bootm: add support for elf file loading
mips: lib: bootm: use bootm elf loading capabilities
arch/mips/lib/bootm.c | 25 ++--- common/Kconfig | 8 ++ common/bootm.c | 33 ++++++ common/elf.c | 231 +++++++++++++++++++++++++++++++++++------- include/bootm.h | 3 + include/elf.h | 16 ++- 6 files changed, 261 insertions(+), 55 deletions(-)
Signature · 7 lines
2.17.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH v6 1/8 common: elf: add computation of elf boundaries
In order to correctly load an initrd or a device tree after an elf file, we need to know its boundaries. This commit adds support for that and allow the bootm implementations to use it for memory loading.
--- common/elf.c | 7 +++++++ include/elf.h | 7 +++++++ 2 files changed, 14 insertions(+)
common/elf.c +7 -0
@@ -59,6 +59,11 @@ static int load_elf_phdr_segment(struct elf_image *elf, void *src,if (!p_filesz)return 0 ; +if (dst < elf->low_addr) + elf->low_addr = dst; +if (dst + p_memsz > elf->high_addr) + elf->high_addr = dst + p_memsz; +pr_debug ("Loading phdr to 0x%p (%llu bytes)\n" , dst, p_filesz); ret =elf_request_region (elf, (resource_size_t )dst, p_filesz);
@@ -124,6 +129,8 @@ struct elf_image *elf_load_image(void *buf)INIT_LIST_HEAD (&elf->list); elf->buf = buf; + elf->low_addr = (void *) (unsigned long )-1 ; + elf->high_addr =0 ; ret =elf_check_image (elf);if (ret)
include/elf.h +7 -0
@@ -403,9 +403,16 @@ struct elf_image {struct list_head list;u8 class;u64 entry; +void *low_addr; +void *high_addr;void *buf; }; +static inline size_t elf_get_mem_size (struct elf_image *elf) +{ +return elf->high_addr - elf->low_addr; +} +struct elf_image *elf_load_image (void *buf);void elf_release_image (struct elf_image *elf);
Signature · 7 lines
2.17.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH v6 2/8 common: elf: fix warning on 32 bits architectures
In reply to Clement LegerWhen pointers are 32 bits wide and we cast a potentially 64 bits value in it, the compiler will yield an error. Cast that value first into a phys_addr_t to match the architecture pointer size and then in a void *.
--- common/elf.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
common/elf.c +1 -1
@@ -47,7 +47,7 @@ static void elf_release_regions(struct elf_image *elf)static int load_elf_phdr_segment (struct elf_image *elf,void *src,void *phdr) { -void *dst = (void *)elf_phdr_p_paddr (elf, phdr); +void *dst = (void *) (phys_addr_t )elf_phdr_p_paddr (elf, phdr);int ret;u64 p_filesz =elf_phdr_p_filesz (elf, phdr);u64 p_memsz =elf_phdr_p_memsz (elf, phdr);
Signature · 7 lines
2.17.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH v6 3/8 common: elf: use calloc instead of xzalloc
In reply to Clement LegerThis failure can be handled grecefully, use standard calloc.
--- common/elf.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
common/elf.c +4 -1
@@ -19,7 +19,10 @@ static int elf_request_region(struct elf_image *elf, resource_size_t start,struct resource *r_new;struct elf_section *r; - r =xzalloc (sizeof (*r)); + r =calloc (1 ,sizeof (*r)); +if (!r) +return -ENOMEM ; + r_new =request_sdram_region ("elf_section" , start, size);if (!r_new) {pr_err ("Failed to request region: %pa %pa\n" , &start, &size);
Signature · 7 lines
2.17.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH v6 4/8 common: elf: check number of elf program headers
In reply to Clement LegerAn elf file without program headers should not be loaded. Add a check for such cases.
--- common/elf.c | 5 +++++ 1 file changed, 5 insertions(+)
common/elf.c +5 -0
@@ -119,6 +119,11 @@ static int elf_check_image(struct elf_image *elf)return -ENOEXEC ; } +if (!elf_hdr_e_phnum (elf, elf->buf)) { +pr_err ("No phdr found.\n" ); +return -ENOEXEC ; + } +return 0 ; }
Signature · 7 lines
2.17.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH v6 5/8 common: elf: load elf directly from file
In reply to Clement LegerCurrently, elf file must be loaded into a buffer and then passed to elf_load_image. This requires to copy the whole elf file before booting it. This commit allows to pass the filename directly and will allocate data only for the elf header (elf header + program headers). This will then be used to load the elf data from the file without copying it in an intermediate buffer. Elf segments are first parsed into a list and are then loaded from the file in a second time.
--- arch/mips/lib/bootm.c | 9 +- common/elf.c | 220 +++++++++++++++++++++++++++++++++++------- include/elf.h | 6 +- 3 files changed, 191 insertions(+), 44 deletions(-)
arch/mips/lib/bootm.c +2 -7
@@ -47,14 +47,10 @@ static int do_bootm_elf(struct image_data *data) {void (*entry)(int ,void *);struct elf_image *elf; -void *fdt, *buf; +void *fdt;int ret =0 ; - buf =read_file (data->os_file,NULL ); -if (!buf) -return -EINVAL ; - - elf =elf_load_image (buf); + elf =elf_load_image (data->os_file);if (IS_ERR (elf))return PTR_ERR (elf);
@@ -82,7 +78,6 @@ static int do_bootm_elf(struct image_data *data) bootm_elf_done:elf_release_image (elf);free (fdt); -free (buf);return ret; }
common/elf.c +185 -35
312 lines not on this page · raw message
include/elf.h +4 -2
@@ -405,7 +405,8 @@ struct elf_image {u64 entry;void *low_addr;void *high_addr; -void *buf; +void *hdr_buf; +char *filename; };static inline size_t elf_get_mem_size (struct elf_image *elf)
@@ -413,7 +414,7 @@ static inline size_t elf_get_mem_size(struct elf_image *elf)return elf->high_addr - elf->low_addr; } -struct elf_image *elf_load_image (void *buf); +struct elf_image *elf_load_image (const char *filename);void elf_release_image (struct elf_image *elf);#define ELF_GET_FIELD (__s, __field, __type) \
@@ -427,6 +428,7 @@ static inline __type elf_##__s##_##__field(struct elf_image *elf, void *arg) { \ ELF_GET_FIELD(hdr, e_entry, u64) ELF_GET_FIELD(hdr, e_phnum, u16) ELF_GET_FIELD(hdr, e_phoff, u64) +ELF_GET_FIELD(hdr, e_phentsize, u16) ELF_GET_FIELD(hdr, e_type, u16) ELF_GET_FIELD(phdr, p_paddr, u64)ELF_GET_FIELD (phdr, p_filesz, u64)
Signature · 7 lines
2.17.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH v6 6/8 common: elf: add elf_open, elf_close and elf_load
In reply to Clement LegerIn order to integrate elf loading into bootm command, split elf opening from elf loading.
--- arch/mips/lib/bootm.c | 8 ++++++-- common/elf.c | 20 +++++++------------- include/elf.h | 5 +++-- 3 files changed, 16 insertions(+), 17 deletions(-)
arch/mips/lib/bootm.c +6 -2
@@ -50,10 +50,14 @@ static int do_bootm_elf(struct image_data *data)void *fdt;int ret =0 ; - elf =elf_load_image (data->os_file); + elf =elf_open (data->os_file);if (IS_ERR (elf))return PTR_ERR (elf); + ret =elf_load (elf); +if (ret) + goto bootm_elf_done; + fdt =bootm_get_devicetree (data);if (IS_ERR (fdt)) { ret =PTR_ERR (fdt);
@@ -76,7 +80,7 @@ static int do_bootm_elf(struct image_data *data) ret = -EINVAL ; bootm_elf_done: -elf_release_image (elf); +elf_close (elf);free (fdt);return ret;
common/elf.c +7 -13
@@ -285,23 +285,17 @@ err_free_elf:return ERR_PTR (ret); } -struct elf_image *elf_load_image (const char *filename) +struct elf_image *elf_open (const char *filename) { -int ret; -struct elf_image *elf; - - elf =elf_check_init (filename); -if (IS_ERR (elf)) -return elf; - - ret =load_elf_image_segments (elf); -if (ret) -return ERR_PTR (ret); +return elf_check_init (filename); +} -return elf; +int elf_load (struct elf_image *elf) +{ +return load_elf_image_segments (elf); } -void elf_release_image (struct elf_image *elf) +void elf_close (struct elf_image *elf) {elf_release_regions (elf);
include/elf.h +3 -2
@@ -414,8 +414,9 @@ static inline size_t elf_get_mem_size(struct elf_image *elf)return elf->high_addr - elf->low_addr; } -struct elf_image *elf_load_image (const char *filename); -void elf_release_image (struct elf_image *elf); +struct elf_image *elf_open (const char *filename); +void elf_close (struct elf_image *elf); +int elf_load (struct elf_image *elf);#define ELF_GET_FIELD (__s, __field, __type) \ static inline __type elf_##__s##_##__field(struct elf_image *elf, void *arg) { \
Signature · 7 lines
2.17.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH v6 7/8 common: bootm: add support for elf file loading
In reply to Clement LegerThis will allows elf loader to directly have an elf file available. Thus filetype_elf bootm handlers will be able to use standard bootm functions and data.
--- common/Kconfig | 8 ++++++++ common/bootm.c | 33 +++++++++++++++++++++++++++++++++ include/bootm.h | 3 +++ 3 files changed, 44 insertions(+)
common/Kconfig +8 -0
@@ -597,6 +597,14 @@ config BOOTM_AIMAGE help Support using Android Images. +config BOOTM_ELF + bool + depends on BOOTM + select ELF + prompt "elf loading support" + help + Add support to load elf file with bootm. + config BOOTM_FITIMAGE bool prompt "FIT image support"
common/bootm.c +33 -0
@@ -143,6 +143,9 @@ int bootm_load_os(struct image_data *data, unsigned long load_address)return 0 ; } +if (IS_ENABLED (CONFIG_ELF ) && data->elf) +return elf_load (data->elf); +if (data->os_file) { data->os_res =file_to_sdram (data->os_file, load_address);if (!data->os_res)
@@ -470,6 +473,8 @@ int bootm_get_os_size(struct image_data *data) {int ret; +if (data->elf) +return elf_get_mem_size (data->elf);if (data->os)return uimage_get_size (data->os,uimage_part_num (data->os_part));if (data->os_fit)
@@ -517,6 +522,22 @@ static int bootm_open_os_uimage(struct image_data *data)return 0 ; } +static int bootm_open_elf (struct image_data *data) +{ +if (!IS_ENABLED (CONFIG_ELF )) +return -ENOSYS ; + + data->elf =elf_open (data->os_file); +if (IS_ERR (data->elf)) +return PTR_ERR (data->elf); + +printf ("Entry Point: %08llx\n" , data->elf->entry); + + data->os_address = data->elf->entry; + +return 0 ; +} +static void bootm_print_info (struct image_data *data) {if (data->os_res)
@@ -652,6 +673,16 @@ int bootm_boot(struct bootm_data *bootm_data) } } +if (os_type == filetype_elf) { + ret =bootm_open_elf (data); +if (ret) { +printf ("Loading ELF image failed with: %s\n" , +strerror (-ret)); + data->elf =NULL ; + goto err_out; + } + } +if (bootm_data->appendroot) {char *rootarg;
@@ -721,6 +752,8 @@ err_out:uimage_close (data->initrd);if (data->os)uimage_close (data->os); +if (IS_ENABLED (CONFIG_ELF ) && data->elf) +elf_close (data->elf);if (IS_ENABLED (CONFIG_FITIMAGE ) && data->os_fit)fit_close (data->os_fit);if (data->of_root_node && data->of_root_node !=of_get_root_node ())
include/bootm.h +3 -0
@@ -46,6 +46,9 @@ struct image_data {/* if os is an uImage this will be provided */ struct uimage_handle *os; +/* if os is an elf file this will be provided */ +struct elf_image *elf; +/* if os is a FIT image this will be provided */ struct fit_handle *os_fit;
Signature · 7 lines
2.17.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
PATCH v6 8/8 mips: lib: bootm: use bootm elf loading capabilities
In reply to Clement LegerNow that the elf file is loaded by the bootm core, there is no need for elf pointer anymore. Thus all elf related fields can be removed and bootm_load_os can be used.
--- arch/mips/lib/bootm.c | 20 +++++++------------- 1 file changed, 7 insertions(+), 13 deletions(-)
arch/mips/lib/bootm.c +7 -13
@@ -46,41 +46,35 @@ static struct binfmt_hook binfmt_barebox_hook = {static int do_bootm_elf (struct image_data *data) {void (*entry)(int ,void *); -struct elf_image *elf;void *fdt;int ret =0 ; - elf =elf_open (data->os_file); -if (IS_ERR (elf)) -return PTR_ERR (elf); - - ret =elf_load (elf); + ret =bootm_load_os (data, data->os_address);if (ret) - goto bootm_elf_done; +return ret; fdt =bootm_get_devicetree (data);if (IS_ERR (fdt)) { ret =PTR_ERR (fdt); - goto bootm_elf_done; + goto bootm_free_fdt; }pr_info ("Starting application at 0x%08lx, dts 0x%08lx...\n" , -phys_to_virt (elf->entry), data->of_root_node); +phys_to_virt (data->os_address), data->of_root_node);if (data->dryrun) - goto bootm_elf_done; + goto bootm_free_fdt;shutdown_barebox (); - entry = (void *) (unsigned long ) elf->entry; + entry = (void *) (unsigned long ) data->os_address;entry (-2 ,phys_to_virt ((unsigned long )fdt));pr_err ("ELF application terminated\n" ); ret = -EINVAL ; -bootm_elf_done: -elf_close (elf); +bootm_free_fdt:free (fdt);return ret;
Signature · 7 lines
2.17.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Nice work. Thank you!
Following variants was tested:
- net boot
- boot from local ram copy
- boot from spi flash
Quoted 66 lines · Clement Leger
Currently, when booting an elf file using "bootm
/dev/mtdx", bootm will simply pass the file to the bootm handler and the read done on it will read the entire flash partition. This series starts by some cleanup and then modify the elf loader to load the elf file without copying the whole elf file to an intermediate buffer. Only the elf header is copied in order to parse the elf file into a list of segment and then, the segments are loaded directly from the file.A special handling for the elf file is also added in bootm data to allow using directly the elf file structure. Finally the mips bootm handler is modified to use
bootm_load_osdirectly instead of manual elf loading.Compilation for both mips and arm has been tested but run on qemu-malta was not possible. Changes have been tested on kvx architecture for which bootm support has been added and will be submitted.
Changes v5 -> v6
- Add missing elf boundaries computation patchChanges v4 -> v5
- Rework elf loading to load from file rather than a buffer
- Use calloc instead of xzalloc in elf file loading
- Add check of program headers in elf checkChanges v3 -> v4
- Fix init of elf entry address to be used bybootm_load_elfChanges v2 -> v3
- Integrate elf loading inbootm_load_os
- Add patch to remove now unused elf_load_image/elf_release_image
- Use malloc instead of xmalloc and check return valueChanges v1 -> v2
- AddBOOTM_ELFconfig to select elf support and add checks in code
- Add anelf_get_mem_sizefunction to avoid computing elf size inbootm.c
- Use xmalloc andread_fullinelf_openinstead of xzalloc/read
- Fixdata->elfNULL reset
- Remove elf struct entirely from mips bootm codeClement Leger (8):
common: elf: add computation of elf boundaries
common: elf: fix warning on 32 bits architectures
common: elf: use calloc instead of xzalloc
common: elf: check number of elf program headers
common: elf: load elf directly from file
common: elf: addelf_open,elf_closeandelf_load
common: bootm: add support for elf file loading
mips: lib: bootm: use bootm elf loading capabilitiesarch/mips/lib/bootm.c | 25 ++--- common/Kconfig | 8 ++ common/bootm.c | 33 ++++++ common/elf.c | 231 +++++++++++++++++++++++++++++++++++------- include/bootm.h | 3 + include/elf.h | 16 ++- 6 files changed, 261 insertions(+), 55 deletions(-)Signature · 7 lines
2.17.1 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Signature · 4 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Quoted 7 lines · ----- On 12 Jun, 2020, at 09:41, Oleksij Rempel [email protected] wrote:
Nice work. Thank you!
Following variants was tested:
- net boot
- boot from local ram copy
- boot from spi flashTested-by Oleksij Rempel <[email protected]>
Finally ;) Thanks again for testing it on mips !
Quoted 69 lines · Clement Leger
Quoted 57 lines
Currently, when booting an elf file using "bootm
/dev/mtdx", bootm will simply pass the file to the bootm handler and the read done on it will read the entire flash partition. This series starts by some cleanup and then modify the elf loader to load the elf file without copying the whole elf file to an intermediate buffer. Only the elf header is copied in order to parse the elf file into a list of segment and then, the segments are loaded directly from the file.A special handling for the elf file is also added in bootm data to allow using directly the elf file structure. Finally the mips bootm handler is modified to use
bootm_load_osdirectly instead of manual elf loading.Compilation for both mips and arm has been tested but run on qemu-malta was not possible. Changes have been tested on kvx architecture for which bootm support has been added and will be submitted.
Changes v5 -> v6
- Add missing elf boundaries computation patchChanges v4 -> v5
- Rework elf loading to load from file rather than a buffer
- Use calloc instead of xzalloc in elf file loading
- Add check of program headers in elf checkChanges v3 -> v4
- Fix init of elf entry address to be used bybootm_load_elfChanges v2 -> v3
- Integrate elf loading inbootm_load_os
- Add patch to remove now unused elf_load_image/elf_release_image
- Use malloc instead of xmalloc and check return valueChanges v1 -> v2
- AddBOOTM_ELFconfig to select elf support and add checks in code
- Add anelf_get_mem_sizefunction to avoid computing elf size inbootm.c
- Use xmalloc andread_fullinelf_openinstead of xzalloc/read
- Fixdata->elfNULL reset
- Remove elf struct entirely from mips bootm codeClement Leger (8):
common: elf: add computation of elf boundaries
common: elf: fix warning on 32 bits architectures
common: elf: use calloc instead of xzalloc
common: elf: check number of elf program headers
common: elf: load elf directly from file
common: elf: addelf_open,elf_closeandelf_load
common: bootm: add support for elf file loading
mips: lib: bootm: use bootm elf loading capabilitiesarch/mips/lib/bootm.c | 25 ++--- common/Kconfig | 8 ++ common/bootm.c | 33 ++++++ common/elf.c | 231 +++++++++++++++++++++++++++++++++++------- include/bootm.h | 3 + include/elf.h | 16 ++- 6 files changed, 261 insertions(+), 55 deletions(-)Quoted 9 lines
--
2.17.1_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Quoted 5 lines
--
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 |
_______________________________________________
barebox mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/barebox
Sascha Hauer
AppliedQuoted 12 lines · Clement Leger
Currently, when booting an elf file using "bootm
/dev/mtdx", bootm will simply pass the file to the bootm handler and the read done on it will read the entire flash partition. This series starts by some cleanup and then modify the elf loader to load the elf file without copying the whole elf file to an intermediate buffer. Only the elf header is copied in order to parse the elf file into a list of segment and then, the segments are loaded directly from the file.A special handling for the elf file is also added in bootm data to allow using directly the elf file structure. Finally the mips bootm handler is modified to use
bootm_load_osdirectly instead of manual elf loading.
Quoted 6 lines
Compilation for both mips and arm has been tested but run on qemu-malta was not possible. Changes have been tested on kvx architecture for which bootm support has been added and will be submitted.
Changes v5 -> v6
- Add missing elf boundaries computation patch
Applied, thanks
Sascha
Signature · 9 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox