Kenneth Waters | c889fb4 | 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 | |
Kenneth Waters | c889fb4 | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 11 | #include <command.h> |
Simon Glass | 7942bdc | 2022-07-30 15:52:15 -0600 | [diff] [blame] | 12 | #include <mapmem.h> |
Kenneth Waters | c889fb4 | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 13 | #include <part.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 14 | #include <vsprintf.h> |
Kenneth Waters | c889fb4 | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 15 | |
Rasmus Villemoes | c614b83 | 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 | c889fb4 | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 18 | { |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 19 | struct blk_desc *dev_desc = NULL; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 20 | struct disk_partition part_info; |
Rasmus Villemoes | d505840 | 2023-03-02 09:12:21 +0100 | [diff] [blame] | 21 | ulong offset, limit; |
Rasmus Villemoes | c614b83 | 2023-03-02 09:12:22 +0100 | [diff] [blame] | 22 | uint blk, cnt, res; |
Simon Glass | 4c5bc2b | 2024-09-01 16:26:29 -0600 | [diff] [blame] | 23 | void *ptr; |
Rasmus Villemoes | d505840 | 2023-03-02 09:12:21 +0100 | [diff] [blame] | 24 | int part; |
Kenneth Waters | c889fb4 | 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 | d505840 | 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 | c889fb4 | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 34 | return 1; |
Kenneth Waters | c889fb4 | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 35 | |
Simon Glass | 4c5bc2b | 2024-09-01 16:26:29 -0600 | [diff] [blame] | 36 | ptr = map_sysmem(hextoul(argv[3], NULL), 0); |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 37 | blk = hextoul(argv[4], NULL); |
| 38 | cnt = hextoul(argv[5], NULL); |
Kenneth Waters | c889fb4 | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 39 | |
Rasmus Villemoes | d505840 | 2023-03-02 09:12:21 +0100 | [diff] [blame] | 40 | if (part > 0) { |
Kenneth Waters | c889fb4 | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 41 | offset = part_info.start; |
| 42 | limit = part_info.size; |
| 43 | } else { |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 44 | /* Largest address not available in struct blk_desc. */ |
Rasmus Villemoes | d505840 | 2023-03-02 09:12:21 +0100 | [diff] [blame] | 45 | offset = 0; |
Kenneth Waters | c889fb4 | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 46 | limit = ~0; |
| 47 | } |
| 48 | |
| 49 | if (cnt + blk > limit) { |
Rasmus Villemoes | c614b83 | 2023-03-02 09:12:22 +0100 | [diff] [blame] | 50 | printf("%s out of range\n", cmdtp->name); |
Simon Glass | 4c5bc2b | 2024-09-01 16:26:29 -0600 | [diff] [blame] | 51 | unmap_sysmem(ptr); |
Kenneth Waters | c889fb4 | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 52 | return 1; |
| 53 | } |
| 54 | |
Rasmus Villemoes | c614b83 | 2023-03-02 09:12:22 +0100 | [diff] [blame] | 55 | if (IS_ENABLED(CONFIG_CMD_WRITE) && !strcmp(cmdtp->name, "write")) |
Simon Glass | 4c5bc2b | 2024-09-01 16:26:29 -0600 | [diff] [blame] | 56 | res = blk_dwrite(dev_desc, offset + blk, cnt, ptr); |
Rasmus Villemoes | c614b83 | 2023-03-02 09:12:22 +0100 | [diff] [blame] | 57 | else |
Simon Glass | 4c5bc2b | 2024-09-01 16:26:29 -0600 | [diff] [blame] | 58 | res = blk_dread(dev_desc, offset + blk, cnt, ptr); |
| 59 | unmap_sysmem(ptr); |
Rasmus Villemoes | c614b83 | 2023-03-02 09:12:22 +0100 | [diff] [blame] | 60 | |
| 61 | if (res != cnt) { |
| 62 | printf("%s error\n", cmdtp->name); |
Kenneth Waters | c889fb4 | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 63 | return 1; |
| 64 | } |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
Rasmus Villemoes | c614b83 | 2023-03-02 09:12:22 +0100 | [diff] [blame] | 69 | #ifdef CONFIG_CMD_READ |
Kenneth Waters | c889fb4 | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 70 | U_BOOT_CMD( |
Rasmus Villemoes | c614b83 | 2023-03-02 09:12:22 +0100 | [diff] [blame] | 71 | read, 6, 0, do_rw, |
Kenneth Waters | c889fb4 | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 72 | "Load binary data from a partition", |
Rasmus Villemoes | d505840 | 2023-03-02 09:12:21 +0100 | [diff] [blame] | 73 | "<interface> <dev[:part|#partname]> addr blk# cnt" |
Kenneth Waters | c889fb4 | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 74 | ); |
Rasmus Villemoes | c614b83 | 2023-03-02 09:12:22 +0100 | [diff] [blame] | 75 | #endif |
| 76 | |
| 77 | #ifdef CONFIG_CMD_WRITE |
| 78 | U_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 |