blob: 1218e7acfd0a3bd2b876b010a997a51506be1a3e [file] [log] [blame]
Kenneth Watersff048ea2012-12-05 14:46:30 +00001/*
2 * Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
5 *
6 * Alternatively, this software may be distributed under the terms of the
7 * GNU General Public License ("GPL") version 2 as published by the Free
8 * Software Foundation.
9 */
10
11#include <common.h>
12#include <command.h>
Simon Glasse1410752022-07-30 15:52:15 -060013#include <mapmem.h>
Kenneth Watersff048ea2012-12-05 14:46:30 +000014#include <part.h>
15
Rasmus Villemoes8311ac52023-03-02 09:12:22 +010016static int
17do_rw(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Kenneth Watersff048ea2012-12-05 14:46:30 +000018{
Simon Glass4101f682016-02-29 15:25:34 -070019 struct blk_desc *dev_desc = NULL;
Simon Glass05289792020-05-10 11:39:57 -060020 struct disk_partition part_info;
Rasmus Villemoesfca7db52023-03-02 09:12:21 +010021 ulong offset, limit;
Rasmus Villemoes8311ac52023-03-02 09:12:22 +010022 uint blk, cnt, res;
Kenneth Watersff048ea2012-12-05 14:46:30 +000023 void *addr;
Rasmus Villemoesfca7db52023-03-02 09:12:21 +010024 int part;
Kenneth Watersff048ea2012-12-05 14:46:30 +000025
26 if (argc != 6) {
27 cmd_usage(cmdtp);
28 return 1;
29 }
30
Rasmus Villemoesfca7db52023-03-02 09:12:21 +010031 part = part_get_info_by_dev_and_name_or_num(argv[1], argv[2],
32 &dev_desc, &part_info, 1);
33 if (part < 0)
Kenneth Watersff048ea2012-12-05 14:46:30 +000034 return 1;
Kenneth Watersff048ea2012-12-05 14:46:30 +000035
Simon Glasse1410752022-07-30 15:52:15 -060036 addr = map_sysmem(hextoul(argv[3], NULL), 0);
Simon Glass7e5f4602021-07-24 09:03:29 -060037 blk = hextoul(argv[4], NULL);
38 cnt = hextoul(argv[5], NULL);
Kenneth Watersff048ea2012-12-05 14:46:30 +000039
Rasmus Villemoesfca7db52023-03-02 09:12:21 +010040 if (part > 0) {
Kenneth Watersff048ea2012-12-05 14:46:30 +000041 offset = part_info.start;
42 limit = part_info.size;
43 } else {
Simon Glass4101f682016-02-29 15:25:34 -070044 /* Largest address not available in struct blk_desc. */
Rasmus Villemoesfca7db52023-03-02 09:12:21 +010045 offset = 0;
Kenneth Watersff048ea2012-12-05 14:46:30 +000046 limit = ~0;
47 }
48
49 if (cnt + blk > limit) {
Rasmus Villemoes8311ac52023-03-02 09:12:22 +010050 printf("%s out of range\n", cmdtp->name);
Kenneth Watersff048ea2012-12-05 14:46:30 +000051 return 1;
52 }
53
Rasmus Villemoes8311ac52023-03-02 09:12:22 +010054 if (IS_ENABLED(CONFIG_CMD_WRITE) && !strcmp(cmdtp->name, "write"))
55 res = blk_dwrite(dev_desc, offset + blk, cnt, addr);
56 else
57 res = blk_dread(dev_desc, offset + blk, cnt, addr);
58
59 if (res != cnt) {
60 printf("%s error\n", cmdtp->name);
Kenneth Watersff048ea2012-12-05 14:46:30 +000061 return 1;
62 }
63
64 return 0;
65}
66
Rasmus Villemoes8311ac52023-03-02 09:12:22 +010067#ifdef CONFIG_CMD_READ
Kenneth Watersff048ea2012-12-05 14:46:30 +000068U_BOOT_CMD(
Rasmus Villemoes8311ac52023-03-02 09:12:22 +010069 read, 6, 0, do_rw,
Kenneth Watersff048ea2012-12-05 14:46:30 +000070 "Load binary data from a partition",
Rasmus Villemoesfca7db52023-03-02 09:12:21 +010071 "<interface> <dev[:part|#partname]> addr blk# cnt"
Kenneth Watersff048ea2012-12-05 14:46:30 +000072);
Rasmus Villemoes8311ac52023-03-02 09:12:22 +010073#endif
74
75#ifdef CONFIG_CMD_WRITE
76U_BOOT_CMD(
77 write, 6, 0, do_rw,
78 "Store binary data to a partition",
79 "<interface> <dev[:part|#partname]> addr blk# cnt"
80);
81#endif