blob: af54bd176547fa05724e18bfca8cfccd0a2f85e9 [file] [log] [blame]
Kenneth Watersc889fb42012-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
Kenneth Watersc889fb42012-12-05 14:46:30 +000011#include <command.h>
Simon Glass7942bdc2022-07-30 15:52:15 -060012#include <mapmem.h>
Kenneth Watersc889fb42012-12-05 14:46:30 +000013#include <part.h>
Tom Rini5c1444f2024-04-27 08:10:59 -060014#include <vsprintf.h>
Kenneth Watersc889fb42012-12-05 14:46:30 +000015
Rasmus Villemoesc614b832023-03-02 09:12:22 +010016static int
17do_rw(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Kenneth Watersc889fb42012-12-05 14:46:30 +000018{
Simon Glasse3394752016-02-29 15:25:34 -070019 struct blk_desc *dev_desc = NULL;
Simon Glassc1c4a8f2020-05-10 11:39:57 -060020 struct disk_partition part_info;
Rasmus Villemoesd5058402023-03-02 09:12:21 +010021 ulong offset, limit;
Rasmus Villemoesc614b832023-03-02 09:12:22 +010022 uint blk, cnt, res;
Kenneth Watersc889fb42012-12-05 14:46:30 +000023 void *addr;
Rasmus Villemoesd5058402023-03-02 09:12:21 +010024 int part;
Kenneth Watersc889fb42012-12-05 14:46:30 +000025
26 if (argc != 6) {
27 cmd_usage(cmdtp);
28 return 1;
29 }
30
Rasmus Villemoesd5058402023-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 Watersc889fb42012-12-05 14:46:30 +000034 return 1;
Kenneth Watersc889fb42012-12-05 14:46:30 +000035
Simon Glass7942bdc2022-07-30 15:52:15 -060036 addr = map_sysmem(hextoul(argv[3], NULL), 0);
Simon Glass3ff49ec2021-07-24 09:03:29 -060037 blk = hextoul(argv[4], NULL);
38 cnt = hextoul(argv[5], NULL);
Kenneth Watersc889fb42012-12-05 14:46:30 +000039
Rasmus Villemoesd5058402023-03-02 09:12:21 +010040 if (part > 0) {
Kenneth Watersc889fb42012-12-05 14:46:30 +000041 offset = part_info.start;
42 limit = part_info.size;
43 } else {
Simon Glasse3394752016-02-29 15:25:34 -070044 /* Largest address not available in struct blk_desc. */
Rasmus Villemoesd5058402023-03-02 09:12:21 +010045 offset = 0;
Kenneth Watersc889fb42012-12-05 14:46:30 +000046 limit = ~0;
47 }
48
49 if (cnt + blk > limit) {
Rasmus Villemoesc614b832023-03-02 09:12:22 +010050 printf("%s out of range\n", cmdtp->name);
Kenneth Watersc889fb42012-12-05 14:46:30 +000051 return 1;
52 }
53
Rasmus Villemoesc614b832023-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 Watersc889fb42012-12-05 14:46:30 +000061 return 1;
62 }
63
64 return 0;
65}
66
Rasmus Villemoesc614b832023-03-02 09:12:22 +010067#ifdef CONFIG_CMD_READ
Kenneth Watersc889fb42012-12-05 14:46:30 +000068U_BOOT_CMD(
Rasmus Villemoesc614b832023-03-02 09:12:22 +010069 read, 6, 0, do_rw,
Kenneth Watersc889fb42012-12-05 14:46:30 +000070 "Load binary data from a partition",
Rasmus Villemoesd5058402023-03-02 09:12:21 +010071 "<interface> <dev[:part|#partname]> addr blk# cnt"
Kenneth Watersc889fb42012-12-05 14:46:30 +000072);
Rasmus Villemoesc614b832023-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