blob: 8e21f004423e824dd619882b9f910a082950b41a [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 Rinidec7ea02024-05-20 13:35:03 -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;
Simon Glass4c5bc2b2024-09-01 16:26:29 -060023 void *ptr;
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 Glass4c5bc2b2024-09-01 16:26:29 -060036 ptr = 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);
Simon Glass4c5bc2b2024-09-01 16:26:29 -060051 unmap_sysmem(ptr);
Kenneth Watersc889fb42012-12-05 14:46:30 +000052 return 1;
53 }
54
Rasmus Villemoesc614b832023-03-02 09:12:22 +010055 if (IS_ENABLED(CONFIG_CMD_WRITE) && !strcmp(cmdtp->name, "write"))
Simon Glass4c5bc2b2024-09-01 16:26:29 -060056 res = blk_dwrite(dev_desc, offset + blk, cnt, ptr);
Rasmus Villemoesc614b832023-03-02 09:12:22 +010057 else
Simon Glass4c5bc2b2024-09-01 16:26:29 -060058 res = blk_dread(dev_desc, offset + blk, cnt, ptr);
59 unmap_sysmem(ptr);
Rasmus Villemoesc614b832023-03-02 09:12:22 +010060
61 if (res != cnt) {
62 printf("%s error\n", cmdtp->name);
Kenneth Watersc889fb42012-12-05 14:46:30 +000063 return 1;
64 }
65
66 return 0;
67}
68
Rasmus Villemoesc614b832023-03-02 09:12:22 +010069#ifdef CONFIG_CMD_READ
Kenneth Watersc889fb42012-12-05 14:46:30 +000070U_BOOT_CMD(
Rasmus Villemoesc614b832023-03-02 09:12:22 +010071 read, 6, 0, do_rw,
Kenneth Watersc889fb42012-12-05 14:46:30 +000072 "Load binary data from a partition",
Rasmus Villemoesd5058402023-03-02 09:12:21 +010073 "<interface> <dev[:part|#partname]> addr blk# cnt"
Kenneth Watersc889fb42012-12-05 14:46:30 +000074);
Rasmus Villemoesc614b832023-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