aboutsummaryrefslogtreecommitdiff
path: root/cmd/blkmap.c
blob: ef74ebc0036ce8b3d2314eedf491ce03b1aca42f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// SPDX-License-Identifier: GPL-2.0+
/*
 * Copyright (c) 2023 Addiva Elektronik
 * Author: Tobias Waldekranz <tobias@waldekranz.com>
 */

#include <blk.h>
#include <blkmap.h>
#include <common.h>
#include <command.h>
#include <malloc.h>
#include <dm/device.h>

static int blkmap_curr_dev;

struct map_ctx {
	struct udevice *dev;
	lbaint_t blknr, blkcnt;
};

typedef int (*map_parser_fn)(struct map_ctx *ctx, int argc, char *const argv[]);

struct map_handler {
	const char *name;
	map_parser_fn fn;
};

static int do_blkmap_map_linear(struct map_ctx *ctx, int argc,
				char *const argv[])
{
	struct blk_desc *lbd;
	int err, ldevnum;
	lbaint_t lblknr;

	if (argc < 4)
		return CMD_RET_USAGE;

	ldevnum = dectoul(argv[2], NULL);
	lblknr = dectoul(argv[3], NULL);

	lbd = blk_get_devnum_by_uclass_idname(argv[1], ldevnum);
	if (!lbd) {
		printf("Found no device matching \"%s %d\"\n",
		       argv[1], ldevnum);
		return CMD_RET_FAILURE;
	}

	err = blkmap_map_linear(ctx->dev, ctx->blknr, ctx->blkcnt,
				lbd->bdev, lblknr);
	if (err) {
		printf("Unable to map \"%s %d\" at block 0x" LBAF ": %d\n",
		       argv[1], ldevnum, ctx->blknr, err);

		return CMD_RET_FAILURE;
	}

	printf("Block 0x" LBAF "+0x" LBAF " mapped to block 0x" LBAF " of \"%s %d\"\n",
	       ctx->blknr, ctx->blkcnt, lblknr, argv[1], ldevnum);
	return CMD_RET_SUCCESS;
}

static int do_blkmap_map_mem(struct map_ctx *ctx, int argc, char *const argv[])
{
	phys_addr_t addr;
	int err;

	if (argc < 2)
		return CMD_RET_USAGE;

	addr = hextoul(argv[1], NULL);

	err = blkmap_map_pmem(ctx->dev, ctx->blknr, ctx->blkcnt, addr);
	if (err) {
		printf("Unable to map %#llx at block 0x" LBAF ": %d\n",
		       (unsigned long long)addr, ctx->blknr, err);
		return CMD_RET_FAILURE;
	}

	printf("Block 0x" LBAF "+0x" LBAF " mapped to %#llx\n",
	       ctx->blknr, ctx->blkcnt, (unsigned long long)addr);
	return CMD_RET_SUCCESS;
}

static struct map_handler map_handlers[] = {
	{ .name = "linear", .fn = do_blkmap_map_linear },
	{ .name = "mem", .fn = do_blkmap_map_mem },

	{ .name = NULL }
};

static int do_blkmap_map(struct cmd_tbl *cmdtp, int flag,
			 int argc, char *const argv[])
{
	struct map_handler *handler;
	struct map_ctx ctx;

	if (argc < 5)
		return CMD_RET_USAGE;

	ctx.dev = blkmap_from_label(argv[1]);
	if (!ctx.dev) {
		printf("\"%s\" is not the name of any known blkmap\n", argv[1]);
		return CMD_RET_FAILURE;
	}

	ctx.blknr = hextoul(argv[2], NULL);
	ctx.blkcnt = hextoul(argv[3], NULL);
	argc -= 4;
	argv += 4;

	for (handler = map_handlers; handler->name; handler++) {
		if (!strcmp(handler->name, argv[0]))
			return handler->fn(&ctx, argc, argv);
	}

	printf("Unknown map type \"%s\"\n", argv[0]);
	return CMD_RET_USAGE;
}

static int do_blkmap_create(struct cmd_tbl *cmdtp, int flag,
			    int argc, char *const argv[])
{
	const char *label;
	int err;

	if (argc != 2)
		return CMD_RET_USAGE;

	label = argv[1];

	err = blkmap_create(label, NULL);
	if (err) {
		printf("Unable to create \"%s\": %d\n", label, err);
		return CMD_RET_FAILURE;
	}

	printf("Created \"%s\"\n", label);
	return CMD_RET_SUCCESS;
}

static int do_blkmap_destroy(struct cmd_tbl *cmdtp, int flag,
			     int argc, char *const argv[])
{
	struct udevice *dev;
	const char *label;
	int err;

	if (argc != 2)
		return CMD_RET_USAGE;

	label = argv[1];

	dev = blkmap_from_label(label);
	if (!dev) {
		printf("\"%s\" is not the name of any known blkmap\n", label);
		return CMD_RET_FAILURE;
	}

	err = blkmap_destroy(dev);
	if (err) {
		printf("Unable to destroy \"%s\": %d\n", label, err);
		return CMD_RET_FAILURE;
	}

	printf("Destroyed \"%s\"\n", label);
	return CMD_RET_SUCCESS;
}

static int do_blkmap_get(struct cmd_tbl *cmdtp, int flag,
			 int argc, char *const argv[])
{
	struct udevice *dev;
	const char *label;
	int err;

	if (argc < 3)
		return CMD_RET_USAGE;

	label = argv[1];

	dev = blkmap_from_label(label);
	if (!dev) {
		printf("\"%s\" is not the name of any known blkmap\n", label);
		return CMD_RET_FAILURE;
	}

	if (!strcmp(argv[2], "dev")) {
		if (argc == 3) {
			printf("%d\n", dev_seq(dev));
		} else {
			err = env_set_hex(argv[3], dev_seq(dev));
			if (err)
				return CMD_RET_FAILURE;
		}
	} else {
		return CMD_RET_USAGE;
	}

	return CMD_RET_SUCCESS;
}

static int do_blkmap_common(struct cmd_tbl *cmdtp, int flag,
			    int argc, char *const argv[])
{
	/* The subcommand parsing pops the original argv[0] ("blkmap")
	 * which blk_common_cmd expects. Push it back again.
	 */
	argc++;
	argv--;

	return blk_common_cmd(argc, argv, UCLASS_BLKMAP, &blkmap_curr_dev);
}

U_BOOT_CMD_WITH_SUBCMDS(
	blkmap, "Composeable virtual block devices",
	"info - list configured devices\n"
	"blkmap part - list available partitions on current blkmap device\n"
	"blkmap dev [<dev>] - show or set current blkmap device\n"
	"blkmap read <addr> <blk#> <cnt>\n"
	"blkmap write <addr> <blk#> <cnt>\n"
	"blkmap get <label> dev [<var>] - store device number in variable\n"
	"blkmap create <label> - create device\n"
	"blkmap destroy <label> - destroy device\n"
	"blkmap map <label> <blk#> <cnt> linear <interface> <dev> <blk#> - device mapping\n"
	"blkmap map <label> <blk#> <cnt> mem <addr> - memory mapping\n",
	U_BOOT_SUBCMD_MKENT(info, 2, 1, do_blkmap_common),
	U_BOOT_SUBCMD_MKENT(part, 2, 1, do_blkmap_common),
	U_BOOT_SUBCMD_MKENT(dev, 4, 1, do_blkmap_common),
	U_BOOT_SUBCMD_MKENT(read, 5, 1, do_blkmap_common),
	U_BOOT_SUBCMD_MKENT(write, 5, 1, do_blkmap_common),
	U_BOOT_SUBCMD_MKENT(get, 5, 1, do_blkmap_get),
	U_BOOT_SUBCMD_MKENT(create, 2, 1, do_blkmap_create),
	U_BOOT_SUBCMD_MKENT(destroy, 2, 1, do_blkmap_destroy),
	U_BOOT_SUBCMD_MKENT(map, 32, 1, do_blkmap_map));