Skip to content

Commit eb446ef

Browse files
miquelraynaltrini
authored andcommitted
cmd: nand/sf: isolate legacy code
The 'sf' command is not supposed to rely on the MTD stack, but both 'sf' and 'nand' commands use helpers located in mtd_uboot.c. Despite their location, these functions do not depend at all on the MTD stack. This file (drivers/mtd/mtd_uboot.c) is only compiled if CONFIG_MTD is selected, which is inconsistent with the current situation. Solve this by moving these three functions (which are only used by the above two commands) out of mtd_uboot.c and put them in a C file only compiled with cmd/sf.c and cmd/nand.c. Signed-off-by: Miquel Raynal <[email protected]> [trini: Don't export get_part function now] Signed-off-by: Tom Rini <[email protected]>
1 parent 587f445 commit eb446ef

File tree

9 files changed

+121
-101
lines changed

9 files changed

+121
-101
lines changed

arch/arm/mach-imx/cmd_nandbcb.c

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include <linux/mtd/mtd.h>
2525
#include <nand.h>
2626

27+
#include "../../../cmd/legacy-mtd-utils.h"
28+
2729
#define BF_VAL(v, bf) (((v) & bf##_MASK) >> bf##_OFFSET)
2830
#define GETBIT(v, n) (((v) >> (n)) & 0x1)
2931

cmd/Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ obj-$(CONFIG_CMD_MMC) += mmc.o
9797
obj-$(CONFIG_MP) += mp.o
9898
obj-$(CONFIG_CMD_MTD) += mtd.o
9999
obj-$(CONFIG_CMD_MTDPARTS) += mtdparts.o
100+
ifneq ($(CONFIG_CMD_NAND)$(CONFIG_CMD_SF),)
101+
obj-y += legacy-mtd-utils.o
102+
endif
100103
obj-$(CONFIG_CMD_NAND) += nand.o
101104
obj-$(CONFIG_CMD_NET) += net.o
102105
obj-$(CONFIG_CMD_NVEDIT_EFI) += nvedit_efi.o

cmd/legacy-mtd-utils.c

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
// SPDX-License-Identifier: GPL-2.0+
2+
3+
#include <common.h>
4+
#include <jffs2/jffs2.h>
5+
#include <linux/mtd/mtd.h>
6+
#include <linux/mtd/partitions.h>
7+
#include <linux/string.h>
8+
#include <mtd.h>
9+
10+
static int get_part(const char *partname, int *idx, loff_t *off, loff_t *size,
11+
loff_t *maxsize, int devtype)
12+
{
13+
#ifdef CONFIG_CMD_MTDPARTS
14+
struct mtd_device *dev;
15+
struct part_info *part;
16+
u8 pnum;
17+
int ret;
18+
19+
ret = mtdparts_init();
20+
if (ret)
21+
return ret;
22+
23+
ret = find_dev_and_part(partname, &dev, &pnum, &part);
24+
if (ret)
25+
return ret;
26+
27+
if (dev->id->type != devtype) {
28+
printf("not same typ %d != %d\n", dev->id->type, devtype);
29+
return -1;
30+
}
31+
32+
*off = part->offset;
33+
*size = part->size;
34+
*maxsize = part->size;
35+
*idx = dev->id->num;
36+
37+
return 0;
38+
#else
39+
puts("mtdparts support missing.\n");
40+
return -1;
41+
#endif
42+
}
43+
44+
int mtd_arg_off(const char *arg, int *idx, loff_t *off, loff_t *size,
45+
loff_t *maxsize, int devtype, uint64_t chipsize)
46+
{
47+
if (!str2off(arg, off))
48+
return get_part(arg, idx, off, size, maxsize, devtype);
49+
50+
if (*off >= chipsize) {
51+
puts("Offset exceeds device limit\n");
52+
return -1;
53+
}
54+
55+
*maxsize = chipsize - *off;
56+
*size = *maxsize;
57+
return 0;
58+
}
59+
60+
int mtd_arg_off_size(int argc, char *const argv[], int *idx, loff_t *off,
61+
loff_t *size, loff_t *maxsize, int devtype,
62+
uint64_t chipsize)
63+
{
64+
int ret;
65+
66+
if (argc == 0) {
67+
*off = 0;
68+
*size = chipsize;
69+
*maxsize = *size;
70+
goto print;
71+
}
72+
73+
ret = mtd_arg_off(argv[0], idx, off, size, maxsize, devtype,
74+
chipsize);
75+
if (ret)
76+
return ret;
77+
78+
if (argc == 1)
79+
goto print;
80+
81+
if (!str2off(argv[1], size)) {
82+
printf("'%s' is not a number\n", argv[1]);
83+
return -1;
84+
}
85+
86+
if (*size > *maxsize) {
87+
puts("Size exceeds partition or device limit\n");
88+
return -1;
89+
}
90+
91+
print:
92+
printf("device %d ", *idx);
93+
if (*size == chipsize)
94+
puts("whole chip\n");
95+
else
96+
printf("offset 0x%llx, size 0x%llx\n",
97+
(unsigned long long)*off, (unsigned long long)*size);
98+
return 0;
99+
}

cmd/legacy-mtd-utils.h

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/* SPDX-License-Identifier: GPL-2.0+ */
2+
3+
#ifndef __LEGACY_MTD_UTILS_H
4+
#define __LEGACY_MTD_UTILS_H
5+
6+
int mtd_arg_off(const char *arg, int *idx, loff_t *off, loff_t *size,
7+
loff_t *maxsize, int devtype, uint64_t chipsize);
8+
int mtd_arg_off_size(int argc, char *const argv[], int *idx, loff_t *off,
9+
loff_t *size, loff_t *maxsize, int devtype,
10+
uint64_t chipsize);
11+
12+
#endif /* LEGACY_MTD_UTILS_H */

cmd/nand.c

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
#include <jffs2/jffs2.h>
3131
#include <nand.h>
3232

33+
#include "legacy-mtd-utils.h"
34+
3335
#if defined(CONFIG_CMD_MTDPARTS)
3436

3537
/* partition handling routines */

cmd/sf.c

+2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <asm/io.h>
1919
#include <dm/device-internal.h>
2020

21+
#include "legacy-mtd-utils.h"
22+
2123
static struct spi_flash *flash;
2224

2325
/*

drivers/mtd/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# (C) Copyright 2000-2007
44
# Wolfgang Denk, DENX Software Engineering, [email protected].
55

6-
ifneq (,$(findstring y,$(CONFIG_MTD)$(CONFIG_CMD_NAND)$(CONFIG_CMD_ONENAND)$(CONFIG_CMD_SF)$(CONFIG_CMD_MTD)))
6+
ifneq (,$(findstring y,$(CONFIG_MTD)$(CONFIG_CMD_ONENAND)$(CONFIG_CMD_MTD)))
77
obj-y += mtdcore.o mtd_uboot.o
88
endif
99
obj-$(CONFIG_DM_MTD) += mtd-uclass.o

drivers/mtd/mtd_uboot.c

-94
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <env.h>
88
#include <dm/device.h>
99
#include <dm/uclass-internal.h>
10-
#include <jffs2/jffs2.h> /* LEGACY */
1110
#include <linux/mtd/mtd.h>
1211
#include <linux/mtd/partitions.h>
1312
#include <mtd.h>
@@ -356,96 +355,3 @@ int mtd_probe_devices(void)
356355
return 0;
357356
}
358357
#endif /* defined(CONFIG_MTD_PARTITIONS) */
359-
360-
/* Legacy */
361-
362-
static int get_part(const char *partname, int *idx, loff_t *off, loff_t *size,
363-
loff_t *maxsize, int devtype)
364-
{
365-
#ifdef CONFIG_CMD_MTDPARTS
366-
struct mtd_device *dev;
367-
struct part_info *part;
368-
u8 pnum;
369-
int ret;
370-
371-
ret = mtdparts_init();
372-
if (ret)
373-
return ret;
374-
375-
ret = find_dev_and_part(partname, &dev, &pnum, &part);
376-
if (ret)
377-
return ret;
378-
379-
if (dev->id->type != devtype) {
380-
printf("not same typ %d != %d\n", dev->id->type, devtype);
381-
return -1;
382-
}
383-
384-
*off = part->offset;
385-
*size = part->size;
386-
*maxsize = part->size;
387-
*idx = dev->id->num;
388-
389-
return 0;
390-
#else
391-
puts("mtdparts support missing.\n");
392-
return -1;
393-
#endif
394-
}
395-
396-
int mtd_arg_off(const char *arg, int *idx, loff_t *off, loff_t *size,
397-
loff_t *maxsize, int devtype, uint64_t chipsize)
398-
{
399-
if (!str2off(arg, off))
400-
return get_part(arg, idx, off, size, maxsize, devtype);
401-
402-
if (*off >= chipsize) {
403-
puts("Offset exceeds device limit\n");
404-
return -1;
405-
}
406-
407-
*maxsize = chipsize - *off;
408-
*size = *maxsize;
409-
return 0;
410-
}
411-
412-
int mtd_arg_off_size(int argc, char *const argv[], int *idx, loff_t *off,
413-
loff_t *size, loff_t *maxsize, int devtype,
414-
uint64_t chipsize)
415-
{
416-
int ret;
417-
418-
if (argc == 0) {
419-
*off = 0;
420-
*size = chipsize;
421-
*maxsize = *size;
422-
goto print;
423-
}
424-
425-
ret = mtd_arg_off(argv[0], idx, off, size, maxsize, devtype,
426-
chipsize);
427-
if (ret)
428-
return ret;
429-
430-
if (argc == 1)
431-
goto print;
432-
433-
if (!str2off(argv[1], size)) {
434-
printf("'%s' is not a number\n", argv[1]);
435-
return -1;
436-
}
437-
438-
if (*size > *maxsize) {
439-
puts("Size exceeds partition or device limit\n");
440-
return -1;
441-
}
442-
443-
print:
444-
printf("device %d ", *idx);
445-
if (*size == chipsize)
446-
puts("whole chip\n");
447-
else
448-
printf("offset 0x%llx, size 0x%llx\n",
449-
(unsigned long long)*off, (unsigned long long)*size);
450-
return 0;
451-
}

include/linux/mtd/mtd.h

-6
Original file line numberDiff line numberDiff line change
@@ -588,12 +588,6 @@ struct mtd_info *__mtd_next_device(int i);
588588
(mtd) != NULL; \
589589
(mtd) = __mtd_next_device(mtd->index + 1))
590590

591-
int mtd_arg_off(const char *arg, int *idx, loff_t *off, loff_t *size,
592-
loff_t *maxsize, int devtype, uint64_t chipsize);
593-
int mtd_arg_off_size(int argc, char *const argv[], int *idx, loff_t *off,
594-
loff_t *size, loff_t *maxsize, int devtype,
595-
uint64_t chipsize);
596-
597591
/* drivers/mtd/mtdcore.c */
598592
void mtd_get_len_incl_bad(struct mtd_info *mtd, uint64_t offset,
599593
const uint64_t length, uint64_t *len_incl_bad,

0 commit comments

Comments
 (0)