OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2010, Google Inc. |
| 3 * All rights reserved. |
| 4 * |
| 5 * Redistribution and use in source and binary forms, with or without |
| 6 * modification, are permitted provided that the following conditions are |
| 7 * met: |
| 8 * |
| 9 * * Redistributions of source code must retain the above copyright |
| 10 * notice, this list of conditions and the following disclaimer. |
| 11 * * Redistributions in binary form must reproduce the above |
| 12 * copyright notice, this list of conditions and the following disclaimer |
| 13 * in the documentation and/or other materials provided with the |
| 14 * distribution. |
| 15 * * Neither the name of Google Inc. nor the names of its |
| 16 * contributors may be used to endorse or promote products derived from |
| 17 * this software without specific prior written permission. |
| 18 * |
| 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 * |
| 31 * Alternatively, this software may be distributed under the terms of the |
| 32 * GNU General Public License ("GPL") version 2 as published by the Free |
| 33 * Software Foundation. |
| 34 */ |
| 35 |
| 36 #include <config.h> |
| 37 #include <common.h> |
| 38 #include <part.h> |
| 39 #include <boot_device.h> |
| 40 |
| 41 static struct { |
| 42 block_dev_desc_t *dev_desc; |
| 43 ulong offset, limit; |
| 44 } bootdev_config = { |
| 45 .dev_desc = NULL, |
| 46 .offset = 0u, |
| 47 .limit = 0u |
| 48 }; |
| 49 |
| 50 block_dev_desc_t *get_bootdev(void) |
| 51 { |
| 52 return bootdev_config.dev_desc; |
| 53 } |
| 54 |
| 55 int set_bootdev(char *ifname, int dev, int part) |
| 56 { |
| 57 disk_partition_t part_info; |
| 58 |
| 59 if ((bootdev_config.dev_desc = get_dev(ifname, dev)) == NULL) |
| 60 goto cleanup; /* block device not supported */ |
| 61 |
| 62 /* largest address not available in block_dev_desc_t */ |
| 63 if (part == 0) { |
| 64 bootdev_config.limit = ~0; |
| 65 return 0; |
| 66 } |
| 67 |
| 68 if (get_partition_info(bootdev_config.dev_desc, part, &part_info)) |
| 69 goto cleanup; /* cannot find partition */ |
| 70 |
| 71 bootdev_config.offset = part_info.start; |
| 72 bootdev_config.limit = part_info.size; |
| 73 |
| 74 return 0; |
| 75 |
| 76 cleanup: |
| 77 bootdev_config.dev_desc = NULL; |
| 78 bootdev_config.offset = 0; |
| 79 bootdev_config.limit = 0; |
| 80 |
| 81 return 1; |
| 82 } |
| 83 |
| 84 int BootDeviceReadLBA(uint64_t lba_start, uint64_t lba_count, void *buffer) |
| 85 { |
| 86 block_dev_desc_t *dev_desc; |
| 87 |
| 88 if ((dev_desc = bootdev_config.dev_desc) == NULL) |
| 89 return 1; /* No boot device configured */ |
| 90 |
| 91 if (lba_start + lba_count > bootdev_config.limit) |
| 92 return 1; /* read out of range */ |
| 93 |
| 94 if (dev_desc->block_read(dev_desc->dev, |
| 95 bootdev_config.offset + lba_start, lba_count, |
| 96 buffer) < 0) |
| 97 return 1; /* error reading blocks */ |
| 98 |
| 99 return 0; |
| 100 } |
| 101 |
| 102 int BootDeviceWriteLBA(uint64_t lba_start, uint64_t lba_count, |
| 103 const void *buffer) |
| 104 { |
| 105 block_dev_desc_t *dev_desc; |
| 106 |
| 107 if ((dev_desc = bootdev_config.dev_desc) == NULL) |
| 108 return 1; /* No boot device configured */ |
| 109 |
| 110 if (lba_start + lba_count > bootdev_config.limit) |
| 111 return 1; /* read out of range */ |
| 112 |
| 113 if (dev_desc->block_write(dev_desc->dev, |
| 114 bootdev_config.offset + lba_start, lba_count, |
| 115 buffer) < 0) |
| 116 return 1; /* error reading blocks */ |
| 117 |
| 118 return 0; |
| 119 } |
OLD | NEW |