forked from mirror/openwrt
If parse_fn() callback returns any error code, the entire MTD partition table will be destroyed. Returning "0" indicates that the partition should not be split. This patch fixes the kernel warning when running the initramfs image but there is no image in firmware partition: ``` [ 1.554246] no rootfs found after FIT image in "firmware" [ 1.559686] Failed to parse subpartitions: -19 [ 1.564120] Deleting MTD partitions on "spi0.0": [ 1.568743] Deleting bl2 MTD partition [ 1.572507] ------------[ cut here ]------------ [ 1.577110] WARNING: CPU: 0 PID: 1 at del_gendisk+0x260/0x2c4 [ 1.582855] Modules linked in: [ 1.585902] CPU: 0 UID: 0 PID: 1 Comm: swapper/0 Not tainted 6.12.57 #0 [ 1.592505] Hardware name: AiroPi AX3 (DT) [ 1.596589] pstate: 60400005 (nZCv daif +PAN -UAO -TCO -DIT -SSBS BTYPE=--) [ 1.603538] pc : del_gendisk+0x260/0x2c4 [ 1.607452] lr : del_gendisk+0x1c/0x2c4 [ 1.611280] sp : ffffffc080dab460 [ 1.614583] x29: ffffffc080dab460 x28: ffffffc080d75140 x27: 0000000000000004 [ 1.621711] x26: 0000000000000000 x25: 0000000000000000 x24: ffffffc080b3f3d0 [ 1.628838] x23: ffffffc080cf3d78 x22: ffffffc080cf3da8 x21: ffffff80009b8780 [ 1.635966] x20: ffffffc080cf3e10 x19: ffffff80009d7400 x18: ffffffc080c5ebd8 [ 1.643093] x17: ffffffc080cf00f0 x16: 000000009d594d00 x15: 0000000000000088 [ 1.650221] x14: 0000000000000088 x13: 00000000ffffffea x12: ffffffc080cb6b80 [ 1.657348] x11: 0000000000000040 x10: ffffffc080cf3820 x9 : ffffffc080cf3818 [ 1.664475] x8 : ffffff8000420dd8 x7 : 0000000000000000 x6 : 0000000000000000 [ 1.671601] x5 : ffffff8000420db0 x4 : ffffff8000420dd8 x3 : 0000000000000000 [ 1.678728] x2 : 0000000000000000 x1 : 0000000000000000 x0 : 0000000000000004 [ 1.685855] Call trace: [ 1.688290] del_gendisk+0x260/0x2c4 [ 1.691858] del_mtd_blktrans_dev+0x30/0x144 [ 1.696120] mtdblock_remove_dev+0xc/0x20 [ 1.700119] blktrans_notify_remove+0x88/0xc4 [ 1.704464] del_mtd_device+0x58/0xdc [ 1.708120] __del_mtd_partitions+0x98/0xe0 [ 1.712296] del_mtd_partitions+0x50/0x70 [ 1.716299] add_mtd_partitions+0x94/0x1e0 [ 1.720388] parse_mtd_partitions+0x3d0/0x4d4 [ 1.724736] mtd_device_parse_register+0x170/0x36c [ 1.729518] spi_nor_probe+0x250/0x2cc [ 1.733261] spi_mem_probe+0x68/0xa0 [ 1.736828] spi_probe+0x80/0xe0 [ 1.740051] really_probe+0xb8/0x2a4 [ 1.743617] __driver_probe_device+0x74/0x118 [ 1.747962] driver_probe_device+0x3c/0xe0 [ 1.752049] __device_attach_driver+0xac/0xe8 [ 1.756394] bus_for_each_drv+0x6c/0xb0 [ 1.760224] __device_attach+0x98/0x178 [ 1.764049] device_initial_probe+0x10/0x18 [ 1.768223] bus_probe_device+0xa0/0xa4 [ 1.772051] device_add+0x540/0x724 [ 1.775531] __spi_add_device+0x13c/0x1f8 [ 1.779532] of_register_spi_device+0x44c/0x720 [ 1.784051] spi_register_controller+0x4cc/0x6c0 [ 1.788658] devm_spi_register_controller+0x48/0xa0 [ 1.793525] mtk_spi_probe+0x420/0x720 [ 1.797266] platform_probe+0x64/0xcc [ 1.800920] really_probe+0xb8/0x2a4 [ 1.804485] __driver_probe_device+0x74/0x118 [ 1.808831] driver_probe_device+0x3c/0xe0 [ 1.812916] __driver_attach+0x88/0x154 [ 1.816741] bus_for_each_dev+0x60/0xa0 [ 1.820569] driver_attach+0x20/0x28 [ 1.824138] bus_add_driver+0xdc/0x208 [ 1.827880] driver_register+0x64/0x114 [ 1.831705] __platform_driver_register+0x20/0x30 [ 1.836399] mtk_spi_driver_init+0x18/0x20 [ 1.840486] do_one_initcall+0x4c/0x1f8 [ 1.844313] kernel_init_freeable+0x238/0x290 [ 1.848661] kernel_init+0x1c/0x120 [ 1.852141] ret_from_fork+0x10/0x20 [ 1.855707] ---[ end trace 0000000000000000 ]--- [ 1.860731] Deleting u-boot-env MTD partition [ 1.865431] Deleting factory MTD partition [ 1.869907] Deleting fip MTD partition [ 1.874003] Deleting firmware MTD partition ``` Ref: https://git.kernel.org/pub/scm/linux/kernel/git/stable/linux.git/commit/drivers/mtd/mtdpart.c?h=linux-6.12.y&id=5c2f7727d437cd42033d13ebc8b3d74b9fe65712 Signed-off-by: Shiji Yang <yangshiji66@outlook.com> Link: https://github.com/openwrt/openwrt/pull/20706 Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
365 lines
9.7 KiB
C
365 lines
9.7 KiB
C
/*
|
|
* Copyright (c) 2015 The Linux Foundation
|
|
* Copyright (C) 2014 Gabor Juhos <juhosg@openwrt.org>
|
|
*
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
* copyright notice and this permission notice appear in all copies.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
*/
|
|
|
|
#include <linux/module.h>
|
|
#include <linux/mtd/mtd.h>
|
|
#include <linux/mtd/partitions.h>
|
|
#include <linux/types.h>
|
|
#include <linux/byteorder/generic.h>
|
|
#include <linux/slab.h>
|
|
#include <linux/libfdt.h>
|
|
#include <linux/of_fdt.h>
|
|
|
|
#include "mtdsplit.h"
|
|
|
|
// string macros from git://git.denx.de/u-boot.git/include/image.h
|
|
|
|
#define FIT_IMAGES_PATH "/images"
|
|
#define FIT_DATA_PROP "data"
|
|
#define FIT_DATA_POSITION_PROP "data-position"
|
|
#define FIT_DATA_OFFSET_PROP "data-offset"
|
|
#define FIT_DATA_SIZE_PROP "data-size"
|
|
|
|
// functions from git://git.denx.de/u-boot.git/common/image-fit.c
|
|
|
|
/**
|
|
* fit_image_get_data - get data property and its size for a given component image node
|
|
* @fit: pointer to the FIT format image header
|
|
* @noffset: component image node offset
|
|
* @data: double pointer to void, will hold data property's data address
|
|
* @size: pointer to size_t, will hold data property's data size
|
|
*
|
|
* fit_image_get_data() finds data property in a given component image node.
|
|
* If the property is found its data start address and size are returned to
|
|
* the caller.
|
|
*
|
|
* returns:
|
|
* 0, on success
|
|
* -1, on failure
|
|
*/
|
|
static int fit_image_get_data(const void *fit, int noffset,
|
|
const void **data, size_t *size)
|
|
{
|
|
int len;
|
|
|
|
*data = fdt_getprop(fit, noffset, FIT_DATA_PROP, &len);
|
|
if (*data == NULL) {
|
|
*size = 0;
|
|
return -1;
|
|
}
|
|
|
|
*size = len;
|
|
return 0;
|
|
}
|
|
|
|
|
|
/**
|
|
* Get 'data-offset' property from a given image node.
|
|
*
|
|
* @fit: pointer to the FIT image header
|
|
* @noffset: component image node offset
|
|
* @data_offset: holds the data-offset property
|
|
*
|
|
* returns:
|
|
* 0, on success
|
|
* -ENOENT if the property could not be found
|
|
*/
|
|
static int fit_image_get_data_offset(const void *fit, int noffset, int *data_offset)
|
|
{
|
|
const fdt32_t *val;
|
|
|
|
val = fdt_getprop(fit, noffset, FIT_DATA_OFFSET_PROP, NULL);
|
|
if (!val)
|
|
return -ENOENT;
|
|
|
|
*data_offset = fdt32_to_cpu(*val);
|
|
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Get 'data-position' property from a given image node.
|
|
*
|
|
* @fit: pointer to the FIT image header
|
|
* @noffset: component image node offset
|
|
* @data_position: holds the data-position property
|
|
*
|
|
* returns:
|
|
* 0, on success
|
|
* -ENOENT if the property could not be found
|
|
*/
|
|
static int fit_image_get_data_position(const void *fit, int noffset,
|
|
int *data_position)
|
|
{
|
|
const fdt32_t *val;
|
|
|
|
val = fdt_getprop(fit, noffset, FIT_DATA_POSITION_PROP, NULL);
|
|
if (!val)
|
|
return -ENOENT;
|
|
|
|
*data_position = fdt32_to_cpu(*val);
|
|
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Get 'data-size' property from a given image node.
|
|
*
|
|
* @fit: pointer to the FIT image header
|
|
* @noffset: component image node offset
|
|
* @data_size: holds the data-size property
|
|
*
|
|
* returns:
|
|
* 0, on success
|
|
* -ENOENT if the property could not be found
|
|
*/
|
|
static int fit_image_get_data_size(const void *fit, int noffset, int *data_size)
|
|
{
|
|
const fdt32_t *val;
|
|
|
|
val = fdt_getprop(fit, noffset, FIT_DATA_SIZE_PROP, NULL);
|
|
if (!val)
|
|
return -ENOENT;
|
|
|
|
*data_size = fdt32_to_cpu(*val);
|
|
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* fit_image_get_data_and_size - get data and its size including
|
|
* both embedded and external data
|
|
* @fit: pointer to the FIT format image header
|
|
* @noffset: component image node offset
|
|
* @data: double pointer to void, will hold data property's data address
|
|
* @size: pointer to size_t, will hold data property's data size
|
|
*
|
|
* fit_image_get_data_and_size() finds data and its size including
|
|
* both embedded and external data. If the property is found
|
|
* its data start address and size are returned to the caller.
|
|
*
|
|
* returns:
|
|
* 0, on success
|
|
* otherwise, on failure
|
|
*/
|
|
static int fit_image_get_data_and_size(const void *fit, int noffset,
|
|
const void **data, size_t *size)
|
|
{
|
|
bool external_data = false;
|
|
int offset;
|
|
int len;
|
|
int ret;
|
|
|
|
if (!fit_image_get_data_position(fit, noffset, &offset)) {
|
|
external_data = true;
|
|
} else if (!fit_image_get_data_offset(fit, noffset, &offset)) {
|
|
external_data = true;
|
|
/*
|
|
* For FIT with external data, figure out where
|
|
* the external images start. This is the base
|
|
* for the data-offset properties in each image.
|
|
*/
|
|
offset += ((fdt_totalsize(fit) + 3) & ~3);
|
|
}
|
|
|
|
if (external_data) {
|
|
ret = fit_image_get_data_size(fit, noffset, &len);
|
|
if (!ret) {
|
|
*data = fit + offset;
|
|
*size = len;
|
|
}
|
|
} else {
|
|
ret = fit_image_get_data(fit, noffset, data, size);
|
|
}
|
|
|
|
return ret;
|
|
}
|
|
|
|
static int
|
|
mtdsplit_fit_parse(struct mtd_info *mtd,
|
|
const struct mtd_partition **pparts,
|
|
struct mtd_part_parser_data *data)
|
|
{
|
|
struct device_node *np = mtd_get_of_node(mtd);
|
|
const char *cmdline_match = NULL;
|
|
struct fdt_header hdr;
|
|
size_t hdr_len, retlen;
|
|
size_t offset;
|
|
u32 offset_start = 0;
|
|
size_t fit_offset, fit_size;
|
|
size_t rootfs_offset, rootfs_size;
|
|
size_t data_size, img_total, max_size = 0;
|
|
struct mtd_partition *parts;
|
|
int ret, ndepth, noffset, images_noffset;
|
|
const void *img_data;
|
|
void *fit;
|
|
|
|
of_property_read_string(np, "openwrt,cmdline-match", &cmdline_match);
|
|
if (cmdline_match && !strstr(saved_command_line, cmdline_match))
|
|
return 0;
|
|
|
|
of_property_read_u32(np, "openwrt,fit-offset", &offset_start);
|
|
|
|
hdr_len = sizeof(struct fdt_header);
|
|
|
|
/* Parse the MTD device & search for the FIT image location */
|
|
for(offset = 0; offset + hdr_len <= mtd->size; offset += mtd->erasesize) {
|
|
ret = mtd_read(mtd, offset + offset_start, hdr_len, &retlen, (void*) &hdr);
|
|
if (ret) {
|
|
pr_err("read error in \"%s\" at offset 0x%llx\n",
|
|
mtd->name, (unsigned long long) offset);
|
|
return 0;
|
|
}
|
|
|
|
if (retlen != hdr_len) {
|
|
pr_err("short read in \"%s\"\n", mtd->name);
|
|
return 0;
|
|
}
|
|
|
|
/* Check the magic - see if this is a FIT image */
|
|
if (be32_to_cpu(hdr.magic) != OF_DT_HEADER) {
|
|
pr_debug("no valid FIT image found in \"%s\" at offset %llx\n",
|
|
mtd->name, (unsigned long long) offset);
|
|
continue;
|
|
}
|
|
|
|
/* We found a FIT image. Let's keep going */
|
|
break;
|
|
}
|
|
|
|
fit_offset = offset;
|
|
fit_size = be32_to_cpu(hdr.totalsize);
|
|
|
|
if (fit_size == 0) {
|
|
pr_err("FIT image in \"%s\" at offset %llx has null size\n",
|
|
mtd->name, (unsigned long long) fit_offset);
|
|
return 0;
|
|
}
|
|
|
|
/*
|
|
* Classic uImage.FIT has all data embedded into the FDT
|
|
* data structure. Hence the total size of the image equals
|
|
* the total size of the FDT structure.
|
|
* Modern uImage.FIT may have only references to data in FDT,
|
|
* hence we need to parse FDT structure to find the end of the
|
|
* last external data refernced.
|
|
*/
|
|
if (fit_size > 0x80000) {
|
|
enum mtdsplit_part_type type;
|
|
|
|
/* Search for the rootfs partition after the FIT image */
|
|
ret = mtd_find_rootfs_from(mtd, fit_offset + fit_size + offset_start, mtd->size,
|
|
&rootfs_offset, &type);
|
|
if (ret) {
|
|
pr_info("no rootfs found after FIT image in \"%s\"\n",
|
|
mtd->name);
|
|
return 0;
|
|
}
|
|
|
|
rootfs_size = mtd->size - rootfs_offset;
|
|
|
|
parts = kzalloc(2 * sizeof(*parts), GFP_KERNEL);
|
|
if (!parts)
|
|
return 0;
|
|
|
|
parts[0].name = KERNEL_PART_NAME;
|
|
parts[0].offset = fit_offset;
|
|
parts[0].size = mtd_roundup_to_eb(fit_size + offset_start, mtd);
|
|
|
|
if (type == MTDSPLIT_PART_TYPE_UBI)
|
|
parts[1].name = UBI_PART_NAME;
|
|
else
|
|
parts[1].name = ROOTFS_PART_NAME;
|
|
parts[1].offset = rootfs_offset;
|
|
parts[1].size = rootfs_size;
|
|
|
|
*pparts = parts;
|
|
|
|
return 2;
|
|
} else {
|
|
/* Search for rootfs_data after FIT external data */
|
|
fit = kzalloc(fit_size, GFP_KERNEL);
|
|
ret = mtd_read(mtd, offset, fit_size + offset_start, &retlen, fit);
|
|
if (ret) {
|
|
pr_err("read error in \"%s\" at offset 0x%llx\n",
|
|
mtd->name, (unsigned long long) offset);
|
|
return 0;
|
|
}
|
|
|
|
images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
|
|
if (images_noffset < 0) {
|
|
pr_err("Can't find images parent node '%s' (%s)\n",
|
|
FIT_IMAGES_PATH, fdt_strerror(images_noffset));
|
|
return 0;
|
|
}
|
|
|
|
for (ndepth = 0,
|
|
noffset = fdt_next_node(fit, images_noffset, &ndepth);
|
|
(noffset >= 0) && (ndepth > 0);
|
|
noffset = fdt_next_node(fit, noffset, &ndepth)) {
|
|
if (ndepth == 1) {
|
|
ret = fit_image_get_data_and_size(fit, noffset, &img_data, &data_size);
|
|
if (ret)
|
|
return 0;
|
|
|
|
img_total = data_size + (img_data - fit);
|
|
|
|
max_size = (max_size > img_total) ? max_size : img_total;
|
|
}
|
|
}
|
|
|
|
parts = kzalloc(sizeof(*parts), GFP_KERNEL);
|
|
if (!parts)
|
|
return 0;
|
|
|
|
parts[0].name = ROOTFS_SPLIT_NAME;
|
|
parts[0].offset = fit_offset + mtd_roundup_to_eb(max_size, mtd);
|
|
parts[0].size = mtd->size - parts[0].offset;
|
|
|
|
*pparts = parts;
|
|
|
|
kfree(fit);
|
|
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
static const struct of_device_id mtdsplit_fit_of_match_table[] = {
|
|
{ .compatible = "denx,fit" },
|
|
{},
|
|
};
|
|
|
|
static struct mtd_part_parser uimage_parser = {
|
|
.owner = THIS_MODULE,
|
|
.name = "fit-fw",
|
|
.of_match_table = mtdsplit_fit_of_match_table,
|
|
.parse_fn = mtdsplit_fit_parse,
|
|
.type = MTD_PARSER_TYPE_FIRMWARE,
|
|
};
|
|
|
|
/**************************************************
|
|
* Init
|
|
**************************************************/
|
|
|
|
static int __init mtdsplit_fit_init(void)
|
|
{
|
|
register_mtd_parser(&uimage_parser);
|
|
|
|
return 0;
|
|
}
|
|
|
|
module_init(mtdsplit_fit_init);
|