| /* |
| * Copyright 2010, Google Inc. |
| * All rights reserved. |
| * |
| * Redistribution and use in source and binary forms, with or without |
| * modification, are permitted provided that the following conditions are |
| * met: |
| * |
| * * Redistributions of source code must retain the above copyright |
| * notice, this list of conditions and the following disclaimer. |
| * * Redistributions in binary form must reproduce the above |
| * copyright notice, this list of conditions and the following disclaimer |
| * in the documentation and/or other materials provided with the |
| * distribution. |
| * * Neither the name of Google Inc. nor the names of its |
| * contributors may be used to endorse or promote products derived from |
| * this software without specific prior written permission. |
| * |
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| * |
| * Alternatively, this software may be distributed under the terms of the |
| * GNU General Public License ("GPL") version 2 as published by the Free |
| * Software Foundation. |
| */ |
| |
| #include <config.h> |
| #include <common.h> |
| #include <part.h> |
| #include <boot_device.h> |
| |
| static struct { |
| block_dev_desc_t *dev_desc; |
| ulong offset, limit; |
| } bootdev_config = { |
| .dev_desc = NULL, |
| .offset = 0u, |
| .limit = 0u |
| }; |
| |
| block_dev_desc_t *get_bootdev(void) |
| { |
| return bootdev_config.dev_desc; |
| } |
| |
| int set_bootdev(char *ifname, int dev, int part) |
| { |
| disk_partition_t part_info; |
| |
| if ((bootdev_config.dev_desc = get_dev(ifname, dev)) == NULL) |
| goto cleanup; /* block device not supported */ |
| |
| /* largest address not available in block_dev_desc_t */ |
| if (part == 0) { |
| bootdev_config.limit = ~0; |
| return 0; |
| } |
| |
| if (get_partition_info(bootdev_config.dev_desc, part, &part_info)) |
| goto cleanup; /* cannot find partition */ |
| |
| bootdev_config.offset = part_info.start; |
| bootdev_config.limit = part_info.size; |
| |
| return 0; |
| |
| cleanup: |
| bootdev_config.dev_desc = NULL; |
| bootdev_config.offset = 0; |
| bootdev_config.limit = 0; |
| |
| return 1; |
| } |
| |
| int BootDeviceReadLBA(uint64_t lba_start, uint64_t lba_count, void *buffer) |
| { |
| block_dev_desc_t *dev_desc; |
| |
| if ((dev_desc = bootdev_config.dev_desc) == NULL) |
| return 1; /* No boot device configured */ |
| |
| if (lba_start + lba_count > bootdev_config.limit) |
| return 1; /* read out of range */ |
| |
| if (dev_desc->block_read(dev_desc->dev, |
| bootdev_config.offset + lba_start, lba_count, |
| buffer) < 0) |
| return 1; /* error reading blocks */ |
| |
| return 0; |
| } |
| |
| int BootDeviceWriteLBA(uint64_t lba_start, uint64_t lba_count, |
| const void *buffer) |
| { |
| block_dev_desc_t *dev_desc; |
| |
| if ((dev_desc = bootdev_config.dev_desc) == NULL) |
| return 1; /* No boot device configured */ |
| |
| if (lba_start + lba_count > bootdev_config.limit) |
| return 1; /* read out of range */ |
| |
| if (dev_desc->block_write(dev_desc->dev, |
| bootdev_config.offset + lba_start, lba_count, |
| buffer) < 0) |
| return 1; /* error reading blocks */ |
| |
| return 0; |
| } |