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