block: add partition parser for U-Boot uImage.FIT
Subsystems (3)
PATCH v4 2/5 block: add partition parser for U-Boot uImage.FIT
Introduce a new partition parser for U-Boot's Flattened-Image-Tree (FIT) in order to allow Linux to mount the filesystem part of a uImage.FIT.
uImage.FIT needs to be created with external data and aligned to the system's memory page size. e.g.
mkimage -E -B 0x1000 -p 0x1000 ...
--- MAINTAINERS | 6 + block/partitions/Kconfig | 15 ++ block/partitions/Makefile | 1 + block/partitions/check.h | 4 + block/partitions/core.c | 3 + block/partitions/fit.c | 353 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 382 insertions(+) create mode 100644 block/partitions/fit.c
MAINTAINERS +6 -0
@@ -8044,6 +8044,12 @@ F: Documentation/firmware_class/ F: drivers/base/firmware_loader/ F: include/linux/firmware.h +FIT PARTITION TABLE (uImage.FIT) +M: Daniel Golle <[email protected]> +L: [email protected] +S: Maintained +F: block/partitions/fit.c + FLEXTIMER FTM-QUADDEC DRIVER M: Patrick Havelange <[email protected]> L: [email protected]
block/partitions/Kconfig +15 -0
@@ -103,6 +103,21 @@ config ATARI_PARTITION Say Y here if you would like to use hard disks under Linux which were partitioned under the Atari OS. +config FIT_PARTITION + bool "Flattened-Image-Tree (FIT) partition support" if PARTITION_ADVANCED + default n + select LIBFDT + help + Say Y here if your system needs to mount the filesystem part of + a Flattened-Image-Tree (FIT) image commonly used with Das U-Boot. + + uImage.FIT needs to be created with external data and aligned to + the systems memory page size. e.g. + mkimage -E -B 0x1000 -p 0x1000 ... + + If your system doesn't use U-Boot or you don't need to mount uImage.FIT + filesystem sub-images in Linux, say N. + config IBM_PARTITION bool "IBM disk label and partition support" depends on PARTITION_ADVANCED && S390
block/partitions/Makefile +1 -0
@@ -8,6 +8,7 @@ obj-$(CONFIG_ACORN_PARTITION) += acorn.o obj-$(CONFIG_AMIGA_PARTITION) += amiga.o obj-$(CONFIG_ATARI_PARTITION) += atari.o obj-$(CONFIG_AIX_PARTITION) += aix.o +obj-$(CONFIG_FIT_PARTITION) += fit.o obj-$(CONFIG_CMDLINE_PARTITION) += cmdline.o obj-$(CONFIG_MAC_PARTITION) += mac.o obj-$(CONFIG_LDM_PARTITION) += ldm.o
block/partitions/check.h +4 -0
@@ -57,6 +57,7 @@ int amiga_partition(struct parsed_partitions *state);int atari_partition (struct parsed_partitions *state);int cmdline_partition (struct parsed_partitions *state);int efi_partition (struct parsed_partitions *state); +int fit_partition (struct parsed_partitions *state);int ibm_partition (struct parsed_partitions *);int karma_partition (struct parsed_partitions *state);int ldm_partition (struct parsed_partitions *state);
@@ -67,3 +68,6 @@ int sgi_partition(struct parsed_partitions *state);int sun_partition (struct parsed_partitions *state);int sysv68_partition (struct parsed_partitions *state);int ultrix_partition (struct parsed_partitions *state); + +int parse_fit_partitions (struct parsed_partitions *state,u64 fit_start_sector, +u64 sectors,int *slot,int max_slot,bool add_remain);
block/partitions/core.c +3 -0
@@ -43,6 +43,9 @@ static int (*check_part[])(struct parsed_partitions *) = {#ifdef CONFIG_CMDLINE_PARTITION cmdline_partition,#endif +#ifdef CONFIG_FIT_PARTITION + fit_partition, +#endif #ifdef CONFIG_EFI_PARTITION efi_partition, /* this must come before msdos */#endif
block/partitions/fit.c new file +353 -0
353 lines not on this page · raw message
Signature · 1 line
2.38.1
block/partitions/fit.c line 121
Quoted 9 lines · Daniel Golle
block/partitions/fit.c new file +9
-0+/* map first page */ + page =read_mapping_page ( + mapping, fit_start_sector >> (PAGE_SHIFT -SECTOR_SHIFT ),NULL ); + +if (IS_ERR (page)) +return -EFAULT ; + +if (PageError (page)) +return -EFAULT ;
Why are you checking for PageError? You won't ever get a page with an error back from read_mapping_page(). And you have the real error in 'page', so why return -EFAUlT, which would indicate a problem copying from the user. Also, this is a great place to use the new folio APIs instead of the old page APIs. So:
folio = read_mapping_folio(mapping, fit_start_sector >> PAGE_SECTORS_SHIFT, NULL); if (IS_ERR(folio)) return PTR_ERR(folio);
block/partitions/fit.c line 123
Quoted 1 line
block/partitions/fit.c new file +1
-0+ init_fit =page_address (page);
init_fit = folio_address(folio) + offset_in_folio(folio, fit_start_sector * SECTOR_SIZE);
block/partitions/fit.c line 128
Quoted 4 lines
block/partitions/fit.c new file +4
-0+if (!init_fit) { +put_page (page); +return -EFAULT ; + }
page_address() or folio_address() can't ever return NULL, you should just drop this nonsense check.
... actually, why can't you call read_part_sector() and avoid all of this?
Quoted 27 lines · Matthew Wilcox
block/partitions/fit.c line 121
Quoted 9 lines · Daniel Golle
block/partitions/fit.c new file +9
-0+/* map first page */ + page =read_mapping_page ( + mapping, fit_start_sector >> (PAGE_SHIFT -SECTOR_SHIFT ),NULL ); + +if (IS_ERR (page)) +return -EFAULT ; + +if (PageError (page)) +return -EFAULT ;Why are you checking for PageError? You won't ever get a page with an error back from
read_mapping_page(). And you have the real error in 'page', so why return -EFAUlT, which would indicate a problem copying from the user. Also, this is a great place to use the new folio APIs instead of the old page APIs. So:folio = read_mapping_folio(mapping, fit_start_sector >> PAGE_SECTORS_SHIFT, NULL); if (IS_ERR(folio)) return PTR_ERR(folio);block/partitions/fit.c line 123
Quoted 1 line
block/partitions/fit.c new file +1
-0+ init_fit =page_address (page);init_fit = folio_address(folio) + offset_in_folio(folio, fit_start_sector * SECTOR_SIZE);
Quoted 7 lines
block/partitions/fit.c line 128
Quoted 4 lines
block/partitions/fit.c new file +4
-0+if (!init_fit) { +put_page (page); +return -EFAULT ; + }
page_address()orfolio_address()can't ever return NULL, you should just drop this nonsense check.
Thank you for the pointers, I will implement your suggestions and post v5 after the upcoming weekend.
Quoted 3 lines
... actually, why can't you call
read_part_sector()and avoid all of this?
I've tried that before and the problem is that read_part_sector() returns a pointer to one sector (typically 512 bytes) of data. And this pointer should not be accesses beyond sector boundaries, right? You'd have to call read_part_sector() again for the next sector.
The FIT structure, however, usually exceeds the size of one sector, and having a continous memory area covering the structure as a whole is crucial for libfdt to do its job.
I could, of course, use read_part_sector() to copy all sectors covering the FIT structure into a buffer, but that seemed strange given that read_part_sector() actually used read_mapping_page() (and now uses read_mapping_folio()) internally and then returns a pointer to the offset within the page/folio. So why not read it in one piece in first place instead of having it first split up to sectors by read_part_sector() just to then having to reassemble it into a continous buffer again.
Quoted 14 lines · Daniel Golle
Quoted 2 lines · Matthew Wilcox
... actually, why can't you call
read_part_sector()and avoid all of this?I've tried that before and the problem is that
read_part_sector()returns a pointer to one sector (typically 512 bytes) of data. And this pointer should not be accesses beyond sector boundaries, right? You'd have to callread_part_sector()again for the next sector.The FIT structure, however, usually exceeds the size of one sector, and having a continous memory area covering the structure as a whole is crucial for libfdt to do its job.
Quoted 8 lines
I could, of course, use
read_part_sector()to copy all sectors covering the FIT structure into a buffer, but that seemed strange given thatread_part_sector()actually usedread_mapping_page()(and now usesread_mapping_folio()) internally and then returns a pointer to the offset within the page/folio. So why not read it in one piece in first place instead of having it first split up to sectors byread_part_sector()just to then having to reassemble it into a continous buffer again.
Are you guaranteed that it's "sufficiently" aligned on storage so that it fits entirely within a single page? If not, you'll have to copy it, vmap it, or fix libfdt to handle a segmented buffer.
Quoted 24 lines · Matthew Wilcox
Quoted 14 lines · Daniel Golle
Quoted 2 lines · Matthew Wilcox
... actually, why can't you call
read_part_sector()and avoid all of this?I've tried that before and the problem is that
read_part_sector()returns a pointer to one sector (typically 512 bytes) of data. And this pointer should not be accesses beyond sector boundaries, right? You'd have to callread_part_sector()again for the next sector.The FIT structure, however, usually exceeds the size of one sector, and having a continous memory area covering the structure as a whole is crucial for libfdt to do its job.
Quoted 8 lines
I could, of course, use
read_part_sector()to copy all sectors covering the FIT structure into a buffer, but that seemed strange given thatread_part_sector()actually usedread_mapping_page()(and now usesread_mapping_folio()) internally and then returns a pointer to the offset within the page/folio. So why not read it in one piece in first place instead of having it first split up to sectors byread_part_sector()just to then having to reassemble it into a continous buffer again.
Quoted 3 lines
Are you guaranteed that it's "sufficiently" aligned on storage so that it fits entirely within a single page? If not, you'll have to copy it, vmap it, or fix libfdt to handle a segmented buffer.
Yes, for the uImage.FIT to be usable for the partition parser it has to be page-aligned.
There is a check which makes sure that this is the case:
block/partitions/fit.c line 111
Quoted 3 lines
block/partitions/fit.c new file +3
-0+/* uImage.FIT should be aligned to page boundaries */ +if (fit_start_sector % (1 << (PAGE_SHIFT -SECTOR_SHIFT ))) +return 0 ;
In case of mtdblock or ubiblock devices, the image always starts at offset 0, so this is never a problem.
In case of the image being stored in a GPT partition, one has to make sure that the start sector of the partition is page aligned, otherwise the above check will fail and the partition parser will bail out.
Quoted 33 lines · Daniel Golle
Quoted 24 lines · Matthew Wilcox
Quoted 14 lines · Daniel Golle
Quoted 2 lines · Matthew Wilcox
... actually, why can't you call
read_part_sector()and avoid all of this?I've tried that before and the problem is that
read_part_sector()returns a pointer to one sector (typically 512 bytes) of data. And this pointer should not be accesses beyond sector boundaries, right? You'd have to callread_part_sector()again for the next sector.The FIT structure, however, usually exceeds the size of one sector, and having a continous memory area covering the structure as a whole is crucial for libfdt to do its job.
Quoted 8 lines
I could, of course, use
read_part_sector()to copy all sectors covering the FIT structure into a buffer, but that seemed strange given thatread_part_sector()actually usedread_mapping_page()(and now usesread_mapping_folio()) internally and then returns a pointer to the offset within the page/folio. So why not read it in one piece in first place instead of having it first split up to sectors byread_part_sector()just to then having to reassemble it into a continous buffer again.Quoted 3 lines
Are you guaranteed that it's "sufficiently" aligned on storage so that it fits entirely within a single page? If not, you'll have to copy it, vmap it, or fix libfdt to handle a segmented buffer.
Yes, for the uImage.FIT to be usable for the partition parser it has to be page-aligned.
There is a check which makes sure that this is the case:
Quoted 9 lines
block/partitions/fit.c line 111
Quoted 3 lines
block/partitions/fit.c new file +3
-0+/* uImage.FIT should be aligned to page boundaries */ +if (fit_start_sector % (1 << (PAGE_SHIFT -SECTOR_SHIFT ))) +return 0 ;In case of mtdblock or ubiblock devices, the image always starts at offset 0, so this is never a problem.
In case of the image being stored in a GPT partition, one has to make sure that the start sector of the partition is page aligned, otherwise the above check will fail and the partition parser will bail out.
OK. Then I think open coding it is the right idea, just with all the cruft removed ;-) I looked at extracting parts of read_part_sector() into read_part_page(), but it ended up being a two line function that wasn't terribly useful.