Kenneth Waters | ff048ea | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 1 | /* |
| 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 Glass | e141075 | 2022-07-30 15:52:15 -0600 | [diff] [blame] | 13 | #include <mapmem.h> |
Kenneth Waters | ff048ea | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 14 | #include <part.h> |
| 15 | |
Rasmus Villemoes | 8311ac5 | 2023-03-02 09:12:22 +0100 | [diff] [blame] | 16 | static int |
| 17 | do_rw(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Kenneth Waters | ff048ea | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 18 | { |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 19 | struct blk_desc *dev_desc = NULL; |
Simon Glass | 0528979 | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 20 | struct disk_partition part_info; |
Rasmus Villemoes | fca7db5 | 2023-03-02 09:12:21 +0100 | [diff] [blame] | 21 | ulong offset, limit; |
Rasmus Villemoes | 8311ac5 | 2023-03-02 09:12:22 +0100 | [diff] [blame] | 22 | uint blk, cnt, res; |
Kenneth Waters | ff048ea | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 23 | void *addr; |
Rasmus Villemoes | fca7db5 | 2023-03-02 09:12:21 +0100 | [diff] [blame] | 24 | int part; |
Kenneth Waters | ff048ea | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 25 | |
| 26 | if (argc != 6) { |
| 27 | cmd_usage(cmdtp); |
| 28 | return 1; |
| 29 | } |
| 30 | |
Rasmus Villemoes | fca7db5 | 2023-03-02 09:12:21 +0100 | [diff] [blame] | 31 | 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 Waters | ff048ea | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 34 | return 1; |
Kenneth Waters | ff048ea | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 35 | |
Simon Glass | e141075 | 2022-07-30 15:52:15 -0600 | [diff] [blame] | 36 | addr = map_sysmem(hextoul(argv[3], NULL), 0); |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 37 | blk = hextoul(argv[4], NULL); |
| 38 | cnt = hextoul(argv[5], NULL); |
Kenneth Waters | ff048ea | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 39 | |
Rasmus Villemoes | fca7db5 | 2023-03-02 09:12:21 +0100 | [diff] [blame] | 40 | if (part > 0) { |
Kenneth Waters | ff048ea | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 41 | offset = part_info.start; |
| 42 | limit = part_info.size; |
| 43 | } else { |
Simon Glass | 4101f68 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 44 | /* Largest address not available in struct blk_desc. */ |
Rasmus Villemoes | fca7db5 | 2023-03-02 09:12:21 +0100 | [diff] [blame] | 45 | offset = 0; |
Kenneth Waters | ff048ea | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 46 | limit = ~0; |
| 47 | } |
| 48 | |
| 49 | if (cnt + blk > limit) { |
Rasmus Villemoes | 8311ac5 | 2023-03-02 09:12:22 +0100 | [diff] [blame] | 50 | printf("%s out of range\n", cmdtp->name); |
Kenneth Waters | ff048ea | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 51 | return 1; |
| 52 | } |
| 53 | |
Rasmus Villemoes | 8311ac5 | 2023-03-02 09:12:22 +0100 | [diff] [blame] | 54 | 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 Waters | ff048ea | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 61 | return 1; |
| 62 | } |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
Rasmus Villemoes | 8311ac5 | 2023-03-02 09:12:22 +0100 | [diff] [blame] | 67 | #ifdef CONFIG_CMD_READ |
Kenneth Waters | ff048ea | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 68 | U_BOOT_CMD( |
Rasmus Villemoes | 8311ac5 | 2023-03-02 09:12:22 +0100 | [diff] [blame] | 69 | read, 6, 0, do_rw, |
Kenneth Waters | ff048ea | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 70 | "Load binary data from a partition", |
Rasmus Villemoes | fca7db5 | 2023-03-02 09:12:21 +0100 | [diff] [blame] | 71 | "<interface> <dev[:part|#partname]> addr blk# cnt" |
Kenneth Waters | ff048ea | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 72 | ); |
Rasmus Villemoes | 8311ac5 | 2023-03-02 09:12:22 +0100 | [diff] [blame] | 73 | #endif |
| 74 | |
| 75 | #ifdef CONFIG_CMD_WRITE |
| 76 | U_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 |