1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2021 Rafał Miłecki <rafal@milecki.pl>
4  */
5 
6 #include <linux/bcm47xx_nvram.h>
7 #include <linux/mtd/mtd.h>
8 #include <linux/mtd/partitions.h>
9 
10 #include "ofpart_linksys_ns.h"
11 
12 #define NVRAM_BOOT_PART		"bootpartition"
13 
14 static int ofpart_linksys_ns_bootpartition(void)
15 {
16 	char buf[4];
17 	int bootpartition;
18 
19 	/* Check CFE environment variable */
20 	if (bcm47xx_nvram_getenv(NVRAM_BOOT_PART, buf, sizeof(buf)) > 0) {
21 		if (!kstrtoint(buf, 0, &bootpartition))
22 			return bootpartition;
23 		pr_warn("Failed to parse %s value \"%s\"\n", NVRAM_BOOT_PART,
24 			buf);
25 	} else {
26 		pr_warn("Failed to get NVRAM \"%s\"\n", NVRAM_BOOT_PART);
27 	}
28 
29 	return 0;
30 }
31 
32 int linksys_ns_partitions_post_parse(struct mtd_info *mtd,
33 				     struct mtd_partition *parts,
34 				     int nr_parts)
35 {
36 	int bootpartition = ofpart_linksys_ns_bootpartition();
37 	int trx_idx = 0;
38 	int i;
39 
40 	for (i = 0; i < nr_parts; i++) {
41 		if (of_device_is_compatible(parts[i].of_node, "linksys,ns-firmware")) {
42 			if (trx_idx++ == bootpartition)
43 				parts[i].name = "firmware";
44 			else
45 				parts[i].name = "backup";
46 		}
47 	}
48 
49 	return 0;
50 }
51