Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * Copyright © 2007 Eugene Konev <ejka@openwrt.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * TI AR7 flash partition table.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  * Based on ar7 map by Felix Fietkau <nbd@openwrt.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/mtd/mtd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/mtd/partitions.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/memblock.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <uapi/linux/magic.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) #define AR7_PARTS	4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #define ROOT_OFFSET	0xe0000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #define LOADER_MAGIC1	le32_to_cpu(0xfeedfa42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #define LOADER_MAGIC2	le32_to_cpu(0xfeed1281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) struct ar7_bin_rec {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	unsigned int checksum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 	unsigned int length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	unsigned int address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) static int create_mtd_partitions(struct mtd_info *master,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 				 const struct mtd_partition **pparts,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 				 struct mtd_part_parser_data *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 	struct ar7_bin_rec header;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) 	unsigned int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	size_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	unsigned int pre_size = master->erasesize, post_size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	unsigned int root_offset = ROOT_OFFSET;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 	int retries = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	struct mtd_partition *ar7_parts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	ar7_parts = kcalloc(AR7_PARTS, sizeof(*ar7_parts), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 	if (!ar7_parts)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	ar7_parts[0].name = "loader";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 	ar7_parts[0].offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 	ar7_parts[0].size = master->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 	ar7_parts[0].mask_flags = MTD_WRITEABLE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	ar7_parts[1].name = "config";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	ar7_parts[1].offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	ar7_parts[1].size = master->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 	ar7_parts[1].mask_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 	do { /* Try 10 blocks starting from master->erasesize */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 		offset = pre_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 		mtd_read(master, offset, sizeof(header), &len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 			 (uint8_t *)&header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 		if (!strncmp((char *)&header, "TIENV0.8", 8))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 			ar7_parts[1].offset = pre_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 		if (header.checksum == LOADER_MAGIC1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		if (header.checksum == LOADER_MAGIC2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 		pre_size += master->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 	} while (retries--);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	pre_size = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 	if (!ar7_parts[1].offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		ar7_parts[1].offset = master->size - master->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		post_size = master->erasesize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 	switch (header.checksum) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 	case LOADER_MAGIC1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		while (header.length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 			offset += sizeof(header) + header.length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 			mtd_read(master, offset, sizeof(header), &len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 				 (uint8_t *)&header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 		root_offset = offset + sizeof(header) + 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	case LOADER_MAGIC2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 		while (header.length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 			offset += sizeof(header) + header.length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			mtd_read(master, offset, sizeof(header), &len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 				 (uint8_t *)&header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 		root_offset = offset + sizeof(header) + 4 + 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		root_offset &= ~(uint32_t)0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 		printk(KERN_WARNING "Unknown magic: %08x\n", header.checksum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	mtd_read(master, root_offset, sizeof(header), &len, (u8 *)&header);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 	if (header.checksum != SQUASHFS_MAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 		root_offset += master->erasesize - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) 		root_offset &= ~(master->erasesize - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	ar7_parts[2].name = "linux";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	ar7_parts[2].offset = pre_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) 	ar7_parts[2].size = master->size - pre_size - post_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 	ar7_parts[2].mask_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) 	ar7_parts[3].name = "rootfs";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	ar7_parts[3].offset = root_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	ar7_parts[3].size = master->size - root_offset - post_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	ar7_parts[3].mask_flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	*pparts = ar7_parts;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	return AR7_PARTS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) static struct mtd_part_parser ar7_parser = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	.parse_fn = create_mtd_partitions,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	.name = "ar7part",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) module_mtd_part_parser(ar7_parser);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) MODULE_AUTHOR(	"Felix Fietkau <nbd@openwrt.org>, "
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		"Eugene Konev <ejka@openwrt.org>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) MODULE_DESCRIPTION("MTD partitioning for TI AR7");