treewide: fix replace nbd@openwrt.org with nbd@nbd.name
[openwrt.org/openwrt.git] / target / linux / generic / files / drivers / mtd / mtdsplit / mtdsplit_trx.c
blob:a/target/linux/generic/files/drivers/mtd/mtdsplit/mtdsplit_trx.c -> blob:b/target/linux/generic/files/drivers/mtd/mtdsplit/mtdsplit_trx.c
/* /*
* Copyright (C) 2013 Gabor Juhos <juhosg@openwrt.org> * Copyright (C) 2013 Gabor Juhos <juhosg@openwrt.org>
* Copyright (C) 2014 Felix Fietkau <nbd@openwrt.org> * Copyright (C) 2014 Felix Fietkau <nbd@nbd.name>
* *
* This program is free software; you can redistribute it and/or modify it * This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 as published * under the terms of the GNU General Public License version 2 as published
* by the Free Software Foundation. * by the Free Software Foundation.
* *
*/ */
   
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
   
#include <linux/module.h> #include <linux/module.h>
#include <linux/init.h> #include <linux/init.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/slab.h> #include <linux/slab.h>
#include <linux/mtd/mtd.h> #include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h> #include <linux/mtd/partitions.h>
#include <linux/byteorder/generic.h> #include <linux/byteorder/generic.h>
   
#include "mtdsplit.h" #include "mtdsplit.h"
   
#define TRX_MAGIC 0x30524448 /* "HDR0" */ #define TRX_MAGIC 0x30524448 /* "HDR0" */
   
struct trx_header { struct trx_header {
__le32 magic; __le32 magic;
__le32 len; __le32 len;
__le32 crc32; __le32 crc32;
__le32 flag_version; __le32 flag_version;
__le32 offset[4]; __le32 offset[4];
}; };
   
static int static int
read_trx_header(struct mtd_info *mtd, size_t offset, read_trx_header(struct mtd_info *mtd, size_t offset,
struct trx_header *header) struct trx_header *header)
{ {
size_t header_len; size_t header_len;
size_t retlen; size_t retlen;
int ret; int ret;
   
header_len = sizeof(*header); header_len = sizeof(*header);
ret = mtd_read(mtd, offset, header_len, &retlen, ret = mtd_read(mtd, offset, header_len, &retlen,
(unsigned char *) header); (unsigned char *) header);
if (ret) { if (ret) {
pr_debug("read error in \"%s\"\n", mtd->name); pr_debug("read error in \"%s\"\n", mtd->name);
return ret; return ret;
} }
   
if (retlen != header_len) { if (retlen != header_len) {
pr_debug("short read in \"%s\"\n", mtd->name); pr_debug("short read in \"%s\"\n", mtd->name);
return -EIO; return -EIO;
} }
   
return 0; return 0;
} }
   
static int static int
mtdsplit_parse_trx(struct mtd_info *master, mtdsplit_parse_trx(struct mtd_info *master,
struct mtd_partition **pparts, struct mtd_partition **pparts,
struct mtd_part_parser_data *data) struct mtd_part_parser_data *data)
{ {
struct mtd_partition *parts; struct mtd_partition *parts;
struct trx_header hdr; struct trx_header hdr;
int nr_parts; int nr_parts;
size_t offset; size_t offset;
size_t trx_offset; size_t trx_offset;
size_t trx_size = 0; size_t trx_size = 0;
size_t rootfs_offset; size_t rootfs_offset;
size_t rootfs_size = 0; size_t rootfs_size = 0;
int ret; int ret;
   
nr_parts = 2; nr_parts = 2;
parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL); parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL);
if (!parts) if (!parts)
return -ENOMEM; return -ENOMEM;
   
/* find trx image on erase block boundaries */ /* find trx image on erase block boundaries */
for (offset = 0; offset < master->size; offset += master->erasesize) { for (offset = 0; offset < master->size; offset += master->erasesize) {
trx_size = 0; trx_size = 0;
   
ret = read_trx_header(master, offset, &hdr); ret = read_trx_header(master, offset, &hdr);
if (ret) if (ret)
continue; continue;
   
if (hdr.magic != cpu_to_le32(TRX_MAGIC)) { if (hdr.magic != cpu_to_le32(TRX_MAGIC)) {
pr_debug("no valid trx header found in \"%s\" at offset %llx\n", pr_debug("no valid trx header found in \"%s\" at offset %llx\n",
master->name, (unsigned long long) offset); master->name, (unsigned long long) offset);
continue; continue;
} }
   
trx_size = le32_to_cpu(hdr.len); trx_size = le32_to_cpu(hdr.len);
if ((offset + trx_size) > master->size) { if ((offset + trx_size) > master->size) {
pr_debug("trx image exceeds MTD device \"%s\"\n", pr_debug("trx image exceeds MTD device \"%s\"\n",
master->name); master->name);
continue; continue;
} }
break; break;
} }
   
if (trx_size == 0) { if (trx_size == 0) {
pr_debug("no trx header found in \"%s\"\n", master->name); pr_debug("no trx header found in \"%s\"\n", master->name);
ret = -ENODEV; ret = -ENODEV;
goto err; goto err;
} }
   
trx_offset = offset + hdr.offset[0]; trx_offset = offset + hdr.offset[0];
rootfs_offset = offset + hdr.offset[1]; rootfs_offset = offset + hdr.offset[1];
rootfs_size = master->size - rootfs_offset; rootfs_size = master->size - rootfs_offset;
trx_size = rootfs_offset - trx_offset; trx_size = rootfs_offset - trx_offset;
   
if (rootfs_size == 0) { if (rootfs_size == 0) {
pr_debug("no rootfs found in \"%s\"\n", master->name); pr_debug("no rootfs found in \"%s\"\n", master->name);
ret = -ENODEV; ret = -ENODEV;
goto err; goto err;
} }
   
parts[0].name = KERNEL_PART_NAME; parts[0].name = KERNEL_PART_NAME;
parts[0].offset = trx_offset; parts[0].offset = trx_offset;
parts[0].size = trx_size; parts[0].size = trx_size;
   
parts[1].name = ROOTFS_PART_NAME; parts[1].name = ROOTFS_PART_NAME;
parts[1].offset = rootfs_offset; parts[1].offset = rootfs_offset;
parts[1].size = rootfs_size; parts[1].size = rootfs_size;
   
*pparts = parts; *pparts = parts;
return nr_parts; return nr_parts;
   
err: err:
kfree(parts); kfree(parts);
return ret; return ret;
} }
   
static struct mtd_part_parser trx_parser = { static struct mtd_part_parser trx_parser = {
.owner = THIS_MODULE, .owner = THIS_MODULE,
.name = "trx-fw", .name = "trx-fw",
.parse_fn = mtdsplit_parse_trx, .parse_fn = mtdsplit_parse_trx,
.type = MTD_PARSER_TYPE_FIRMWARE, .type = MTD_PARSER_TYPE_FIRMWARE,
}; };
   
static int __init mtdsplit_trx_init(void) static int __init mtdsplit_trx_init(void)
{ {
register_mtd_parser(&trx_parser); register_mtd_parser(&trx_parser);
   
return 0; return 0;
} }
   
module_init(mtdsplit_trx_init); module_init(mtdsplit_trx_init);
   
comments