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 | |
| 11 | #include <common.h> |
| 12 | #include <command.h> |
| 13 | #include <part.h> |
| 14 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 15 | int do_read(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Kenneth Waters | c889fb4 | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 16 | { |
| 17 | char *ep; |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 18 | struct blk_desc *dev_desc = NULL; |
Kenneth Waters | c889fb4 | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 19 | int dev; |
| 20 | int part = 0; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 21 | struct disk_partition part_info; |
Kenneth Waters | c889fb4 | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 22 | ulong offset = 0u; |
| 23 | ulong limit = 0u; |
| 24 | void *addr; |
| 25 | uint blk; |
| 26 | uint cnt; |
| 27 | |
| 28 | if (argc != 6) { |
| 29 | cmd_usage(cmdtp); |
| 30 | return 1; |
| 31 | } |
| 32 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 33 | dev = (int)hextoul(argv[2], &ep); |
Kenneth Waters | c889fb4 | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 34 | if (*ep) { |
| 35 | if (*ep != ':') { |
| 36 | printf("Invalid block device %s\n", argv[2]); |
| 37 | return 1; |
| 38 | } |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 39 | part = (int)hextoul(++ep, NULL); |
Kenneth Waters | c889fb4 | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 40 | } |
| 41 | |
Simon Glass | be1e9bb | 2016-02-29 15:25:42 -0700 | [diff] [blame] | 42 | dev_desc = blk_get_dev(argv[1], dev); |
Kenneth Waters | c889fb4 | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 43 | if (dev_desc == NULL) { |
| 44 | printf("Block device %s %d not supported\n", argv[1], dev); |
| 45 | return 1; |
| 46 | } |
| 47 | |
Simon Glass | 3ff49ec | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 48 | addr = (void *)hextoul(argv[3], NULL); |
| 49 | blk = hextoul(argv[4], NULL); |
| 50 | cnt = hextoul(argv[5], NULL); |
Kenneth Waters | c889fb4 | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 51 | |
| 52 | if (part != 0) { |
Simon Glass | b89a844 | 2016-02-29 15:25:48 -0700 | [diff] [blame] | 53 | if (part_get_info(dev_desc, part, &part_info)) { |
Kenneth Waters | c889fb4 | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 54 | printf("Cannot find partition %d\n", part); |
| 55 | return 1; |
| 56 | } |
| 57 | offset = part_info.start; |
| 58 | limit = part_info.size; |
| 59 | } else { |
Simon Glass | e339475 | 2016-02-29 15:25:34 -0700 | [diff] [blame] | 60 | /* Largest address not available in struct blk_desc. */ |
Kenneth Waters | c889fb4 | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 61 | limit = ~0; |
| 62 | } |
| 63 | |
| 64 | if (cnt + blk > limit) { |
| 65 | printf("Read out of range\n"); |
| 66 | return 1; |
| 67 | } |
| 68 | |
Tom Rini | 60ab606 | 2017-08-14 20:58:50 -0400 | [diff] [blame] | 69 | if (blk_dread(dev_desc, offset + blk, cnt, addr) != cnt) { |
Kenneth Waters | c889fb4 | 2012-12-05 14:46:30 +0000 | [diff] [blame] | 70 | printf("Error reading blocks\n"); |
| 71 | return 1; |
| 72 | } |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | U_BOOT_CMD( |
| 78 | read, 6, 0, do_read, |
| 79 | "Load binary data from a partition", |
| 80 | "<interface> <dev[:part]> addr blk# cnt" |
| 81 | ); |