b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2013 Felix Fietkau <nbd@nbd.name> |
| 3 | * Copyright (C) 2013 Gabor Juhos <juhosg@openwrt.org> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 as published |
| 7 | * by the Free Software Foundation. |
| 8 | * |
| 9 | */ |
| 10 | |
| 11 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 12 | |
| 13 | #include <linux/module.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/kernel.h> |
| 16 | #include <linux/slab.h> |
| 17 | #include <linux/magic.h> |
| 18 | #include <linux/mtd/mtd.h> |
| 19 | #include <linux/mtd/partitions.h> |
| 20 | #include <linux/byteorder/generic.h> |
| 21 | |
| 22 | #include "mtdsplit.h" |
| 23 | |
| 24 | static int rootfssize; |
| 25 | static __init int setup_show_rootfssize(char *str) |
| 26 | { |
| 27 | int n; |
| 28 | |
| 29 | if (!get_option(&str, &n)) { |
| 30 | rootfssize = 0; |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | rootfssize = n; |
| 35 | |
| 36 | return 1; |
| 37 | } |
| 38 | __setup("rootsize=", setup_show_rootfssize); |
| 39 | |
| 40 | static int |
| 41 | mtdsplit_parse_squashfs(struct mtd_info *master, |
| 42 | const struct mtd_partition **pparts, |
| 43 | struct mtd_part_parser_data *data) |
| 44 | { |
| 45 | struct mtd_partition *part; |
| 46 | struct mtd_info *parent_mtd; |
| 47 | size_t part_offset; |
| 48 | size_t squashfs_len; |
| 49 | int err; |
| 50 | |
| 51 | if (rootfssize) { |
| 52 | squashfs_len = (size_t)rootfssize; |
| 53 | } else { |
| 54 | err = mtd_get_squashfs_len(master, 0, &squashfs_len); |
| 55 | if (err) |
| 56 | return err; |
| 57 | } |
| 58 | |
| 59 | parent_mtd = mtd_get_master(master); |
| 60 | part_offset = mtdpart_get_offset(master); |
| 61 | |
| 62 | part = kzalloc(sizeof(*part), GFP_KERNEL); |
| 63 | if (!part) { |
| 64 | pr_alert("unable to allocate memory for \"%s\" partition\n", |
| 65 | ROOTFS_SPLIT_NAME); |
| 66 | return -ENOMEM; |
| 67 | } |
| 68 | |
| 69 | part->name = ROOTFS_SPLIT_NAME; |
| 70 | part->offset = mtd_roundup_to_eb(part_offset + squashfs_len, |
| 71 | parent_mtd) - part_offset; |
| 72 | part->size = mtd_rounddown_to_eb(master->size - part->offset, master); |
| 73 | |
| 74 | *pparts = part; |
| 75 | return 1; |
| 76 | } |
| 77 | |
| 78 | static struct mtd_part_parser mtdsplit_squashfs_parser = { |
| 79 | .owner = THIS_MODULE, |
| 80 | .name = "squashfs-split", |
| 81 | .parse_fn = mtdsplit_parse_squashfs, |
| 82 | .type = MTD_PARSER_TYPE_ROOTFS, |
| 83 | }; |
| 84 | |
| 85 | static int __init mtdsplit_squashfs_init(void) |
| 86 | { |
| 87 | register_mtd_parser(&mtdsplit_squashfs_parser); |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | subsys_initcall(mtdsplit_squashfs_init); |