1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2003 - 2004
4  * Sysgo Real-Time Solutions, AG <www.elinos.com>
5  * Pavel Bartusek <pba@sysgo.com>
6  */
7 
8 /*
9  * Reiserfs support
10  */
11 #include <common.h>
12 #include <config.h>
13 #include <command.h>
14 #include <env.h>
15 #include <image.h>
16 #include <linux/ctype.h>
17 #include <asm/byteorder.h>
18 #include <reiserfs.h>
19 #include <part.h>
20 
21 #if !CONFIG_IS_ENABLED(DOS_PARTITION)
22 #error DOS partition support must be selected
23 #endif
24 
25 /* #define	REISER_DEBUG */
26 
27 #ifdef	REISER_DEBUG
28 #define	PRINTF(fmt,args...)	printf (fmt ,##args)
29 #else
30 #define PRINTF(fmt,args...)
31 #endif
32 
do_reiserls(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])33 int do_reiserls(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
34 {
35 	char *filename = "/";
36 	int dev, part;
37 	struct blk_desc *dev_desc = NULL;
38 	struct disk_partition info;
39 
40 	if (argc < 3)
41 		return CMD_RET_USAGE;
42 
43 	part = blk_get_device_part_str(argv[1], argv[2], &dev_desc, &info, 1);
44 	if (part < 0)
45 		return 1;
46 
47 	if (argc == 4) {
48 	    filename = argv[3];
49 	}
50 
51 	dev = dev_desc->devnum;
52 	PRINTF("Using device %s %d:%d, directory: %s\n", argv[1], dev, part, filename);
53 
54 	reiserfs_set_blk_dev(dev_desc, &info);
55 
56 	if (!reiserfs_mount(info.size)) {
57 		printf ("** Bad Reiserfs partition or disk - %s %d:%d **\n",  argv[1], dev, part);
58 		return 1;
59 	}
60 
61 	if (reiserfs_ls (filename)) {
62 		printf ("** Error reiserfs_ls() **\n");
63 		return 1;
64 	};
65 
66 	return 0;
67 }
68 
69 U_BOOT_CMD(
70 	reiserls,	4,	1,	do_reiserls,
71 	"list files in a directory (default /)",
72 	"<interface> <dev[:part]> [directory]\n"
73 	"    - list files from 'dev' on 'interface' in a 'directory'"
74 );
75 
76 /******************************************************************************
77  * Reiserfs boot command intepreter. Derived from diskboot
78  */
do_reiserload(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])79 int do_reiserload(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
80 {
81 	char *filename = NULL;
82 	int dev, part;
83 	ulong addr = 0, filelen;
84 	struct disk_partition info;
85 	struct blk_desc *dev_desc = NULL;
86 	unsigned long count;
87 	char *addr_str;
88 
89 	switch (argc) {
90 	case 3:
91 		addr_str = env_get("loadaddr");
92 		if (addr_str != NULL) {
93 			addr = simple_strtoul (addr_str, NULL, 16);
94 		} else {
95 			addr = CONFIG_SYS_LOAD_ADDR;
96 		}
97 		filename = env_get("bootfile");
98 		count = 0;
99 		break;
100 	case 4:
101 		addr = simple_strtoul (argv[3], NULL, 16);
102 		filename = env_get("bootfile");
103 		count = 0;
104 		break;
105 	case 5:
106 		addr = simple_strtoul (argv[3], NULL, 16);
107 		filename = argv[4];
108 		count = 0;
109 		break;
110 	case 6:
111 		addr = simple_strtoul (argv[3], NULL, 16);
112 		filename = argv[4];
113 		count = simple_strtoul (argv[5], NULL, 16);
114 		break;
115 
116 	default:
117 		return CMD_RET_USAGE;
118 	}
119 
120 	if (!filename) {
121 		puts ("\n** No boot file defined **\n");
122 		return 1;
123 	}
124 
125 	part = blk_get_device_part_str(argv[1], argv[2], &dev_desc, &info, 1);
126 	if (part < 0)
127 		return 1;
128 
129 	dev = dev_desc->devnum;
130 
131 	printf("Loading file \"%s\" from %s device %d%c%c\n",
132 		filename, argv[1], dev,
133 		part ? ':' : ' ', part ? part + '0' : ' ');
134 
135 	reiserfs_set_blk_dev(dev_desc, &info);
136 
137 	if (!reiserfs_mount(info.size)) {
138 		printf ("** Bad Reiserfs partition or disk - %s %d:%d **\n",  argv[1], dev, part);
139 		return 1;
140 	}
141 
142 	filelen = reiserfs_open(filename);
143 	if (filelen < 0) {
144 		printf("** File not found %s **\n", filename);
145 		return 1;
146 	}
147 	if ((count < filelen) && (count != 0)) {
148 	    filelen = count;
149 	}
150 
151 	if (reiserfs_read((char *)addr, filelen) != filelen) {
152 		printf("\n** Unable to read \"%s\" from %s %d:%d **\n", filename, argv[1], dev, part);
153 		return 1;
154 	}
155 
156 	/* Loading ok, update default load address */
157 	image_load_addr = addr;
158 
159 	printf ("\n%ld bytes read\n", filelen);
160 	env_set_hex("filesize", filelen);
161 
162 	return filelen;
163 }
164 
165 U_BOOT_CMD(
166 	reiserload,	6,	0,	do_reiserload,
167 	"load binary file from a Reiser filesystem",
168 	"<interface> <dev[:part]> [addr] [filename] [bytes]\n"
169 	"    - load binary file 'filename' from 'dev' on 'interface'\n"
170 	"      to address 'addr' from dos filesystem"
171 );
172