blob: 8e21f004423e824dd619882b9f910a082950b41a [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
Kenneth Watersff048ea2012-12-05 14:46:30 +000011#include <command.h>
Simon Glasse1410752022-07-30 15:52:15 -060012#include <mapmem.h>
Kenneth Watersff048ea2012-12-05 14:46:30 +000013#include <part.h>
Tom Rini03de3052024-05-20 13:35:03 -060014#include <vsprintf.h>
Kenneth Watersff048ea2012-12-05 14:46:30 +000015
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;
Simon Glass42f5ffb2024-09-01 16:26:29 -060023 void *ptr;
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 Glass42f5ffb2024-09-01 16:26:29 -060036 ptr = 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);
Simon Glass42f5ffb2024-09-01 16:26:29 -060051 unmap_sysmem(ptr);
Kenneth Watersff048ea2012-12-05 14:46:30 +000052 return 1;
53 }
54
Rasmus Villemoes8311ac52023-03-02 09:12:22 +010055 if (IS_ENABLED(CONFIG_CMD_WRITE) && !strcmp(cmdtp->name, "write"))
Simon Glass42f5ffb2024-09-01 16:26:29 -060056 res = blk_dwrite(dev_desc, offset + blk, cnt, ptr);
Rasmus Villemoes8311ac52023-03-02 09:12:22 +010057 else
Simon Glass42f5ffb2024-09-01 16:26:29 -060058 res = blk_dread(dev_desc, offset + blk, cnt, ptr);
59 unmap_sysmem(ptr);
Rasmus Villemoes8311ac52023-03-02 09:12:22 +010060
61 if (res != cnt) {
62 printf("%s error\n", cmdtp->name);
Kenneth Watersff048ea2012-12-05 14:46:30 +000063 return 1;
64 }
65
66 return 0;
67}
68
Rasmus Villemoes8311ac52023-03-02 09:12:22 +010069#ifdef CONFIG_CMD_READ
Kenneth Watersff048ea2012-12-05 14:46:30 +000070U_BOOT_CMD(
Rasmus Villemoes8311ac52023-03-02 09:12:22 +010071 read, 6, 0, do_rw,
Kenneth Watersff048ea2012-12-05 14:46:30 +000072 "Load binary data from a partition",
Rasmus Villemoesfca7db52023-03-02 09:12:21 +010073 "<interface> <dev[:part|#partname]> addr blk# cnt"
Kenneth Watersff048ea2012-12-05 14:46:30 +000074);
Rasmus Villemoes8311ac52023-03-02 09:12:22 +010075#endif
76
77#ifdef CONFIG_CMD_WRITE
78U_BOOT_CMD(
79 write, 6, 0, do_rw,
80 "Store binary data to a partition",
81 "<interface> <dev[:part|#partname]> addr blk# cnt"
82);
83#endif