2N/A/*
2N/A * GRUB -- GRand Unified Bootloader
2N/A * Copyright (C) 2002,2003,2004,2006,2007,2008,2009,2010 Free Software Foundation, Inc.
2N/A *
2N/A * GRUB is free software: you can redistribute it and/or modify
2N/A * it under the terms of the GNU General Public License as published by
2N/A * the Free Software Foundation, either version 3 of the License, or
2N/A * (at your option) any later version.
2N/A *
2N/A * GRUB is distributed in the hope that it will be useful,
2N/A * but WITHOUT ANY WARRANTY; without even the implied warranty of
2N/A * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2N/A * GNU General Public License for more details.
2N/A *
2N/A * You should have received a copy of the GNU General Public License
2N/A * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
2N/A */
2N/A
2N/A#include <grub/disk.h>
2N/A#include <grub/err.h>
2N/A#include <grub/mm.h>
2N/A#include <grub/types.h>
2N/A#include <grub/partition.h>
2N/A#include <grub/misc.h>
2N/A#include <grub/time.h>
2N/A#include <grub/file.h>
2N/A
2N/A#define GRUB_CACHE_TIMEOUT 2
2N/A
2N/A/* The last time the disk was used. */
2N/Astatic grub_uint64_t grub_last_time = 0;
2N/A
2N/A
2N/A/* Disk cache. */
2N/Astruct grub_disk_cache
2N/A{
2N/A enum grub_disk_dev_id dev_id;
2N/A unsigned long disk_id;
2N/A grub_disk_addr_t sector;
2N/A char *data;
2N/A int lock;
2N/A};
2N/A
2N/Astatic struct grub_disk_cache grub_disk_cache_table[GRUB_DISK_CACHE_NUM];
2N/A
2N/Avoid (*grub_disk_firmware_fini) (void);
2N/Aint grub_disk_firmware_is_tainted;
2N/A
2N/A#if DISK_CACHE_STATS
2N/Astatic unsigned long grub_disk_cache_hits;
2N/Astatic unsigned long grub_disk_cache_misses;
2N/A
2N/Avoid
2N/Agrub_disk_cache_get_performance (unsigned long *hits, unsigned long *misses)
2N/A{
2N/A *hits = grub_disk_cache_hits;
2N/A *misses = grub_disk_cache_misses;
2N/A}
2N/A#endif
2N/A
2N/Astatic unsigned
2N/Agrub_disk_cache_get_index (unsigned long dev_id, unsigned long disk_id,
2N/A grub_disk_addr_t sector)
2N/A{
2N/A return ((dev_id * 524287UL + disk_id * 2606459UL
2N/A + ((unsigned) (sector >> GRUB_DISK_CACHE_BITS)))
2N/A % GRUB_DISK_CACHE_NUM);
2N/A}
2N/A
2N/Astatic void
2N/Agrub_disk_cache_invalidate (unsigned long dev_id, unsigned long disk_id,
2N/A grub_disk_addr_t sector)
2N/A{
2N/A unsigned index;
2N/A struct grub_disk_cache *cache;
2N/A
2N/A sector &= ~(GRUB_DISK_CACHE_SIZE - 1);
2N/A index = grub_disk_cache_get_index (dev_id, disk_id, sector);
2N/A cache = grub_disk_cache_table + index;
2N/A
2N/A if (cache->dev_id == dev_id && cache->disk_id == disk_id
2N/A && cache->sector == sector && cache->data)
2N/A {
2N/A cache->lock = 1;
2N/A grub_free (cache->data);
2N/A cache->data = 0;
2N/A cache->lock = 0;
2N/A }
2N/A}
2N/A
2N/Avoid
2N/Agrub_disk_cache_invalidate_all (void)
2N/A{
2N/A unsigned i;
2N/A
2N/A for (i = 0; i < GRUB_DISK_CACHE_NUM; i++)
2N/A {
2N/A struct grub_disk_cache *cache = grub_disk_cache_table + i;
2N/A
2N/A if (cache->data && ! cache->lock)
2N/A {
2N/A grub_free (cache->data);
2N/A cache->data = 0;
2N/A }
2N/A }
2N/A}
2N/A
2N/Astatic char *
2N/Agrub_disk_cache_fetch (unsigned long dev_id, unsigned long disk_id,
2N/A grub_disk_addr_t sector)
2N/A{
2N/A struct grub_disk_cache *cache;
2N/A unsigned index;
2N/A
2N/A index = grub_disk_cache_get_index (dev_id, disk_id, sector);
2N/A cache = grub_disk_cache_table + index;
2N/A
2N/A if (cache->dev_id == dev_id && cache->disk_id == disk_id
2N/A && cache->sector == sector)
2N/A {
2N/A cache->lock = 1;
2N/A#if DISK_CACHE_STATS
2N/A grub_disk_cache_hits++;
2N/A#endif
2N/A return cache->data;
2N/A }
2N/A
2N/A#if DISK_CACHE_STATS
2N/A grub_disk_cache_misses++;
2N/A#endif
2N/A
2N/A return 0;
2N/A}
2N/A
2N/Astatic void
2N/Agrub_disk_cache_unlock (unsigned long dev_id, unsigned long disk_id,
2N/A grub_disk_addr_t sector)
2N/A{
2N/A struct grub_disk_cache *cache;
2N/A unsigned index;
2N/A
2N/A index = grub_disk_cache_get_index (dev_id, disk_id, sector);
2N/A cache = grub_disk_cache_table + index;
2N/A
2N/A if (cache->dev_id == dev_id && cache->disk_id == disk_id
2N/A && cache->sector == sector)
2N/A cache->lock = 0;
2N/A}
2N/A
2N/Astatic grub_err_t
2N/Agrub_disk_cache_store (unsigned long dev_id, unsigned long disk_id,
2N/A grub_disk_addr_t sector, const char *data)
2N/A{
2N/A unsigned index;
2N/A struct grub_disk_cache *cache;
2N/A
2N/A index = grub_disk_cache_get_index (dev_id, disk_id, sector);
2N/A cache = grub_disk_cache_table + index;
2N/A
2N/A cache->lock = 1;
2N/A grub_free (cache->data);
2N/A cache->data = 0;
2N/A cache->lock = 0;
2N/A
2N/A cache->data = grub_malloc (GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS);
2N/A if (! cache->data)
2N/A return grub_errno;
2N/A
2N/A grub_memcpy (cache->data, data,
2N/A GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS);
2N/A cache->dev_id = dev_id;
2N/A cache->disk_id = disk_id;
2N/A cache->sector = sector;
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/A
2N/A
2N/Astatic grub_disk_dev_t grub_disk_dev_list;
2N/A
2N/Avoid
2N/Agrub_disk_dev_register (grub_disk_dev_t dev)
2N/A{
2N/A dev->next = grub_disk_dev_list;
2N/A grub_disk_dev_list = dev;
2N/A}
2N/A
2N/Avoid
2N/Agrub_disk_dev_unregister (grub_disk_dev_t dev)
2N/A{
2N/A grub_disk_dev_t *p, q;
2N/A
2N/A for (p = &grub_disk_dev_list, q = *p; q; p = &(q->next), q = q->next)
2N/A if (q == dev)
2N/A {
2N/A *p = q->next;
2N/A break;
2N/A }
2N/A}
2N/A
2N/Aint
2N/Agrub_disk_dev_iterate (int (*hook) (const char *name))
2N/A{
2N/A grub_disk_dev_t p;
2N/A grub_disk_pull_t pull;
2N/A
2N/A for (pull = 0; pull < GRUB_DISK_PULL_MAX; pull++)
2N/A for (p = grub_disk_dev_list; p; p = p->next)
2N/A if (p->iterate && (p->iterate) (hook, pull))
2N/A return 1;
2N/A
2N/A return 0;
2N/A}
2N/A
2N/A/* Return the location of the first ',', if any, which is not
2N/A escaped by a '\'. */
2N/Astatic const char *
2N/Afind_part_sep (const char *name)
2N/A{
2N/A const char *p = name;
2N/A char c;
2N/A
2N/A while ((c = *p++) != '\0')
2N/A {
2N/A if (c == '\\' && *p == ',')
2N/A p++;
2N/A else if (c == ',')
2N/A return p - 1;
2N/A }
2N/A return NULL;
2N/A}
2N/A
2N/Agrub_disk_t
2N/Agrub_disk_open (const char *name)
2N/A{
2N/A const char *p;
2N/A grub_disk_t disk;
2N/A grub_disk_dev_t dev;
2N/A char *raw = (char *) name;
2N/A grub_uint64_t current_time;
2N/A
2N/A grub_dprintf ("disk", "Opening `%s'...\n", name);
2N/A
2N/A disk = (grub_disk_t) grub_zalloc (sizeof (*disk));
2N/A if (! disk)
2N/A return 0;
2N/A disk->log_sector_size = GRUB_DISK_SECTOR_BITS;
2N/A
2N/A p = find_part_sep (name);
2N/A if (p)
2N/A {
2N/A grub_size_t len = p - name;
2N/A
2N/A raw = grub_malloc (len + 1);
2N/A if (! raw)
2N/A goto fail;
2N/A
2N/A grub_memcpy (raw, name, len);
2N/A raw[len] = '\0';
2N/A disk->name = grub_strdup (raw);
2N/A }
2N/A else
2N/A disk->name = grub_strdup (name);
2N/A if (! disk->name)
2N/A goto fail;
2N/A
2N/A for (dev = grub_disk_dev_list; dev; dev = dev->next)
2N/A {
2N/A if ((dev->open) (raw, disk) == GRUB_ERR_NONE)
2N/A break;
2N/A else if (grub_errno == GRUB_ERR_UNKNOWN_DEVICE)
2N/A grub_errno = GRUB_ERR_NONE;
2N/A else
2N/A goto fail;
2N/A }
2N/A
2N/A if (! dev)
2N/A {
2N/A grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no such disk");
2N/A goto fail;
2N/A }
2N/A if (disk->log_sector_size > GRUB_DISK_CACHE_BITS + GRUB_DISK_SECTOR_BITS
2N/A || disk->log_sector_size < GRUB_DISK_SECTOR_BITS)
2N/A {
2N/A grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
2N/A "sector sizes of %d bytes aren't supported yet",
2N/A (1 << disk->log_sector_size));
2N/A goto fail;
2N/A }
2N/A
2N/A disk->dev = dev;
2N/A
2N/A if (p)
2N/A {
2N/A disk->partition = grub_partition_probe (disk, p + 1);
2N/A if (! disk->partition)
2N/A {
2N/A grub_error (GRUB_ERR_UNKNOWN_DEVICE, "no such partition");
2N/A goto fail;
2N/A }
2N/A }
2N/A
2N/A /* The cache will be invalidated about 2 seconds after a device was
2N/A closed. */
2N/A current_time = grub_get_time_ms ();
2N/A
2N/A if (current_time > (grub_last_time
2N/A + GRUB_CACHE_TIMEOUT * 1000))
2N/A grub_disk_cache_invalidate_all ();
2N/A
2N/A grub_last_time = current_time;
2N/A
2N/A fail:
2N/A
2N/A if (raw && raw != name)
2N/A grub_free (raw);
2N/A
2N/A if (grub_errno != GRUB_ERR_NONE)
2N/A {
2N/A grub_error_push ();
2N/A grub_dprintf ("disk", "Opening `%s' failed.\n", name);
2N/A grub_error_pop ();
2N/A
2N/A grub_disk_close (disk);
2N/A return 0;
2N/A }
2N/A
2N/A return disk;
2N/A}
2N/A
2N/Avoid
2N/Agrub_disk_close (grub_disk_t disk)
2N/A{
2N/A grub_partition_t part;
2N/A grub_dprintf ("disk", "Closing `%s'.\n", disk->name);
2N/A
2N/A if (disk->dev && disk->dev->close)
2N/A (disk->dev->close) (disk);
2N/A
2N/A /* Reset the timer. */
2N/A grub_last_time = grub_get_time_ms ();
2N/A
2N/A while (disk->partition)
2N/A {
2N/A part = disk->partition->parent;
2N/A grub_free (disk->partition);
2N/A disk->partition = part;
2N/A }
2N/A grub_free ((void *) disk->name);
2N/A grub_free (disk);
2N/A}
2N/A
2N/A/* This function performs three tasks:
2N/A - Make sectors disk relative from partition relative.
2N/A - Normalize offset to be less than the sector size.
2N/A - Verify that the range is inside the partition. */
2N/Astatic grub_err_t
2N/Agrub_disk_adjust_range (grub_disk_t disk, grub_disk_addr_t *sector,
2N/A grub_off_t *offset, grub_size_t size)
2N/A{
2N/A grub_partition_t part;
2N/A *sector += *offset >> GRUB_DISK_SECTOR_BITS;
2N/A *offset &= GRUB_DISK_SECTOR_SIZE - 1;
2N/A
2N/A for (part = disk->partition; part; part = part->parent)
2N/A {
2N/A grub_disk_addr_t start;
2N/A grub_uint64_t len;
2N/A
2N/A start = part->start;
2N/A len = part->len;
2N/A
2N/A if (*sector >= len
2N/A || len - *sector < ((*offset + size + GRUB_DISK_SECTOR_SIZE - 1)
2N/A >> GRUB_DISK_SECTOR_BITS))
2N/A return grub_error (GRUB_ERR_OUT_OF_RANGE, "out of partition");
2N/A
2N/A *sector += start;
2N/A }
2N/A
2N/A if (disk->total_sectors != GRUB_DISK_SIZE_UNKNOWN
2N/A && ((disk->total_sectors << (disk->log_sector_size - GRUB_DISK_SECTOR_BITS)) <= *sector
2N/A || ((*offset + size + GRUB_DISK_SECTOR_SIZE - 1)
2N/A >> GRUB_DISK_SECTOR_BITS) > (disk->total_sectors
2N/A << (disk->log_sector_size
2N/A - GRUB_DISK_SECTOR_BITS)) - *sector))
2N/A return grub_error (GRUB_ERR_OUT_OF_RANGE, "out of disk");
2N/A
2N/A return GRUB_ERR_NONE;
2N/A}
2N/A
2N/Astatic inline grub_disk_addr_t
2N/Atransform_sector (grub_disk_t disk, grub_disk_addr_t sector)
2N/A{
2N/A return sector >> (disk->log_sector_size - GRUB_DISK_SECTOR_BITS);
2N/A}
2N/A
2N/A/* Small read (less than cache size and not pass across cache unit boundaries).
2N/A sector is already adjusted and is divisible by cache unit size.
2N/A */
2N/Astatic grub_err_t
2N/Agrub_disk_read_small (grub_disk_t disk, grub_disk_addr_t sector,
2N/A grub_off_t offset, grub_size_t size, void *buf)
2N/A{
2N/A char *data;
2N/A char *tmp_buf;
2N/A
2N/A /* Fetch the cache. */
2N/A data = grub_disk_cache_fetch (disk->dev->id, disk->id, sector);
2N/A if (data)
2N/A {
2N/A /* Just copy it! */
2N/A grub_memcpy (buf, data + offset, size);
2N/A grub_disk_cache_unlock (disk->dev->id, disk->id, sector);
2N/A return GRUB_ERR_NONE;
2N/A }
2N/A
2N/A /* Allocate a temporary buffer. */
2N/A tmp_buf = grub_malloc (GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS);
2N/A if (! tmp_buf)
2N/A return grub_errno;
2N/A
2N/A /* Otherwise read data from the disk actually. */
2N/A if (disk->total_sectors == GRUB_DISK_SIZE_UNKNOWN
2N/A || sector + GRUB_DISK_CACHE_SIZE
2N/A < (disk->total_sectors << (disk->log_sector_size - GRUB_DISK_SECTOR_BITS)))
2N/A {
2N/A grub_err_t err;
2N/A err = (disk->dev->read) (disk, transform_sector (disk, sector),
2N/A 1 << (GRUB_DISK_CACHE_BITS
2N/A + GRUB_DISK_SECTOR_BITS
2N/A - disk->log_sector_size), tmp_buf);
2N/A if (!err)
2N/A {
2N/A /* Copy it and store it in the disk cache. */
2N/A grub_memcpy (buf, tmp_buf + offset, size);
2N/A grub_disk_cache_store (disk->dev->id, disk->id,
2N/A sector, tmp_buf);
2N/A grub_free (tmp_buf);
2N/A return GRUB_ERR_NONE;
2N/A }
2N/A }
2N/A
2N/A grub_free (tmp_buf);
2N/A grub_errno = GRUB_ERR_NONE;
2N/A
2N/A {
2N/A /* Uggh... Failed. Instead, just read necessary data. */
2N/A unsigned num;
2N/A grub_disk_addr_t aligned_sector;
2N/A
2N/A sector += (offset >> GRUB_DISK_SECTOR_BITS);
2N/A offset &= ((1 << GRUB_DISK_SECTOR_BITS) - 1);
2N/A aligned_sector = (sector & ~((1 << (disk->log_sector_size
2N/A - GRUB_DISK_SECTOR_BITS))
2N/A - 1));
2N/A offset += ((sector - aligned_sector) << GRUB_DISK_SECTOR_BITS);
2N/A num = ((size + offset + (1 << (disk->log_sector_size))
2N/A - 1) >> (disk->log_sector_size));
2N/A
2N/A tmp_buf = grub_malloc (num << disk->log_sector_size);
2N/A if (!tmp_buf)
2N/A return grub_errno;
2N/A
2N/A if ((disk->dev->read) (disk, transform_sector (disk, aligned_sector),
2N/A num, tmp_buf))
2N/A {
2N/A grub_error_push ();
2N/A grub_dprintf ("disk", "%s read failed\n", disk->name);
2N/A grub_error_pop ();
2N/A grub_free (tmp_buf);
2N/A return grub_errno;
2N/A }
2N/A grub_memcpy (buf, tmp_buf + offset, size);
2N/A grub_free (tmp_buf);
2N/A return GRUB_ERR_NONE;
2N/A }
2N/A}
2N/A
2N/A/* Read data from the disk. */
2N/Agrub_err_t
2N/Agrub_disk_read (grub_disk_t disk, grub_disk_addr_t sector,
2N/A grub_off_t offset, grub_size_t size, void *buf)
2N/A{
2N/A grub_off_t real_offset;
2N/A grub_disk_addr_t real_sector;
2N/A grub_size_t real_size;
2N/A
2N/A /* First of all, check if the region is within the disk. */
2N/A if (grub_disk_adjust_range (disk, &sector, &offset, size) != GRUB_ERR_NONE)
2N/A {
2N/A grub_error_push ();
2N/A grub_dprintf ("disk", "Read out of range: sector 0x%llx (%s).\n",
2N/A (unsigned long long) sector, grub_errmsg);
2N/A grub_error_pop ();
2N/A return grub_errno;
2N/A }
2N/A
2N/A real_sector = sector;
2N/A real_offset = offset;
2N/A real_size = size;
2N/A
2N/A /* First read until first cache boundary. */
2N/A if (offset || (sector & (GRUB_DISK_CACHE_SIZE - 1)))
2N/A {
2N/A grub_disk_addr_t start_sector;
2N/A grub_size_t pos;
2N/A grub_err_t err;
2N/A grub_size_t len;
2N/A
2N/A start_sector = sector & ~(GRUB_DISK_CACHE_SIZE - 1);
2N/A pos = (sector - start_sector) << GRUB_DISK_SECTOR_BITS;
2N/A len = ((GRUB_DISK_SECTOR_SIZE << GRUB_DISK_CACHE_BITS)
2N/A - pos - offset);
2N/A if (len > size)
2N/A len = size;
2N/A err = grub_disk_read_small (disk, start_sector,
2N/A offset + pos, len, buf);
2N/A if (err)
2N/A return err;
2N/A buf = (char *) buf + len;
2N/A size -= len;
2N/A offset += len;
2N/A sector += (offset >> GRUB_DISK_SECTOR_BITS);
2N/A offset &= ((1 << GRUB_DISK_SECTOR_BITS) - 1);
2N/A }
2N/A
2N/A /* Until SIZE is zero... */
2N/A while (size >= (GRUB_DISK_CACHE_SIZE << GRUB_DISK_SECTOR_BITS))
2N/A {
2N/A char *data = NULL;
2N/A grub_disk_addr_t agglomerate;
2N/A grub_err_t err;
2N/A
2N/A /* agglomerate read until we find a first cached entry. */
2N/A for (agglomerate = 0; agglomerate
2N/A < (size >> (GRUB_DISK_SECTOR_BITS + GRUB_DISK_CACHE_BITS));
2N/A agglomerate++)
2N/A {
2N/A data = grub_disk_cache_fetch (disk->dev->id, disk->id,
2N/A sector + (agglomerate
2N/A << GRUB_DISK_CACHE_BITS));
2N/A if (data)
2N/A break;
2N/A }
2N/A
2N/A if (data)
2N/A {
2N/A grub_memcpy ((char *) buf
2N/A + (agglomerate << (GRUB_DISK_CACHE_BITS
2N/A + GRUB_DISK_SECTOR_BITS)),
2N/A data, GRUB_DISK_CACHE_SIZE << GRUB_DISK_SECTOR_BITS);
2N/A grub_disk_cache_unlock (disk->dev->id, disk->id,
2N/A sector + (agglomerate
2N/A << GRUB_DISK_CACHE_BITS));
2N/A }
2N/A
2N/A if (agglomerate)
2N/A {
2N/A grub_disk_addr_t i;
2N/A
2N/A err = (disk->dev->read) (disk, transform_sector (disk, sector),
2N/A agglomerate << (GRUB_DISK_CACHE_BITS
2N/A + GRUB_DISK_SECTOR_BITS
2N/A - disk->log_sector_size),
2N/A buf);
2N/A if (err)
2N/A return err;
2N/A
2N/A for (i = 0; i < agglomerate; i ++)
2N/A grub_disk_cache_store (disk->dev->id, disk->id,
2N/A sector + (i << GRUB_DISK_CACHE_BITS),
2N/A (char *) buf
2N/A + (i << (GRUB_DISK_CACHE_BITS
2N/A + GRUB_DISK_SECTOR_BITS)));
2N/A
2N/A sector += agglomerate << GRUB_DISK_CACHE_BITS;
2N/A size -= agglomerate << (GRUB_DISK_CACHE_BITS + GRUB_DISK_SECTOR_BITS);
2N/A buf = (char *) buf
2N/A + (agglomerate << (GRUB_DISK_CACHE_BITS + GRUB_DISK_SECTOR_BITS));
2N/A }
2N/A
2N/A if (data)
2N/A {
2N/A sector += GRUB_DISK_CACHE_SIZE;
2N/A buf = (char *) buf + (GRUB_DISK_CACHE_SIZE << GRUB_DISK_SECTOR_BITS);
2N/A size -= (GRUB_DISK_CACHE_SIZE << GRUB_DISK_SECTOR_BITS);
2N/A }
2N/A }
2N/A
2N/A /* And now read the last part. */
2N/A if (size)
2N/A {
2N/A grub_err_t err;
2N/A err = grub_disk_read_small (disk, sector, 0, size, buf);
2N/A if (err)
2N/A return err;
2N/A }
2N/A
2N/A /* Call the read hook, if any. */
2N/A if (disk->read_hook)
2N/A {
2N/A grub_disk_addr_t s = real_sector;
2N/A grub_size_t l = real_size;
2N/A grub_off_t o = real_offset;
2N/A
2N/A while (l)
2N/A {
2N/A grub_size_t cl;
2N/A cl = GRUB_DISK_SECTOR_SIZE - o;
2N/A if (cl > l)
2N/A cl = l;
2N/A (disk->read_hook) (s, o, cl);
2N/A s++;
2N/A l -= cl;
2N/A o = 0;
2N/A }
2N/A }
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/Agrub_err_t
2N/Agrub_disk_write (grub_disk_t disk, grub_disk_addr_t sector,
2N/A grub_off_t offset, grub_size_t size, const void *buf)
2N/A{
2N/A unsigned real_offset;
2N/A grub_disk_addr_t aligned_sector;
2N/A
2N/A grub_dprintf ("disk", "Writing `%s'...\n", disk->name);
2N/A
2N/A if (grub_disk_adjust_range (disk, &sector, &offset, size) != GRUB_ERR_NONE)
2N/A return -1;
2N/A
2N/A aligned_sector = (sector & ~((1 << (disk->log_sector_size
2N/A - GRUB_DISK_SECTOR_BITS)) - 1));
2N/A real_offset = offset + ((sector - aligned_sector) << GRUB_DISK_SECTOR_BITS);
2N/A sector = aligned_sector;
2N/A
2N/A while (size)
2N/A {
2N/A if (real_offset != 0 || (size < (1U << disk->log_sector_size)
2N/A && size != 0))
2N/A {
2N/A char tmp_buf[1 << disk->log_sector_size];
2N/A grub_size_t len;
2N/A grub_partition_t part;
2N/A
2N/A part = disk->partition;
2N/A disk->partition = 0;
2N/A if (grub_disk_read (disk, sector,
2N/A 0, (1 << disk->log_sector_size), tmp_buf)
2N/A != GRUB_ERR_NONE)
2N/A {
2N/A disk->partition = part;
2N/A goto finish;
2N/A }
2N/A disk->partition = part;
2N/A
2N/A len = (1 << disk->log_sector_size) - real_offset;
2N/A if (len > size)
2N/A len = size;
2N/A
2N/A grub_memcpy (tmp_buf + real_offset, buf, len);
2N/A
2N/A grub_disk_cache_invalidate (disk->dev->id, disk->id, sector);
2N/A
2N/A if ((disk->dev->write) (disk, sector, 1, tmp_buf) != GRUB_ERR_NONE)
2N/A goto finish;
2N/A
2N/A sector += (1 << (disk->log_sector_size - GRUB_DISK_SECTOR_BITS));
2N/A buf = (char *) buf + len;
2N/A size -= len;
2N/A real_offset = 0;
2N/A }
2N/A else
2N/A {
2N/A grub_size_t len;
2N/A grub_size_t n;
2N/A
2N/A len = size & ~((1 << disk->log_sector_size) - 1);
2N/A n = size >> disk->log_sector_size;
2N/A
2N/A if ((disk->dev->write) (disk, sector, n, buf) != GRUB_ERR_NONE)
2N/A goto finish;
2N/A
2N/A while (n--)
2N/A grub_disk_cache_invalidate (disk->dev->id, disk->id, sector++);
2N/A
2N/A buf = (char *) buf + len;
2N/A size -= len;
2N/A }
2N/A }
2N/A
2N/A finish:
2N/A
2N/A return grub_errno;
2N/A}
2N/A
2N/Agrub_uint64_t
2N/Agrub_disk_get_size (grub_disk_t disk)
2N/A{
2N/A if (disk->partition)
2N/A return grub_partition_get_len (disk->partition);
2N/A else if (disk->total_sectors != GRUB_DISK_SIZE_UNKNOWN)
2N/A return disk->total_sectors << (disk->log_sector_size - GRUB_DISK_SECTOR_BITS);
2N/A else
2N/A return GRUB_DISK_SIZE_UNKNOWN;
2N/A}