/* * VAS_EBOOT -- GRand Unified Bootloader * Copyright (C) 2002,2003,2004,2006,2007,2008,2009,2010 Free Software Foundation, Inc. * * VAS_EBOOT is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * VAS_EBOOT is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with VAS_EBOOT. If not, see . */ #include #include #include #include #include #include #include #include #include VAS_EBOOT_MOD_LICENSE ("GPLv3+"); #include "../kern/disk_common.c" static void VasEBoot_disk_cache_invalidate (unsigned long dev_id, unsigned long disk_id, VasEBoot_disk_addr_t sector) { unsigned cache_index; struct VasEBoot_disk_cache *cache; sector &= ~((VasEBoot_disk_addr_t) VAS_EBOOT_DISK_CACHE_SIZE - 1); cache_index = VasEBoot_disk_cache_get_index (dev_id, disk_id, sector); cache = VasEBoot_disk_cache_table + cache_index; if (cache->dev_id == dev_id && cache->disk_id == disk_id && cache->sector == sector && cache->data) { cache->lock = 1; VasEBoot_free (cache->data); cache->data = 0; cache->lock = 0; } } VasEBoot_err_t VasEBoot_disk_write (VasEBoot_disk_t disk, VasEBoot_disk_addr_t sector, VasEBoot_off_t offset, VasEBoot_size_t size, const void *buf) { unsigned real_offset; VasEBoot_disk_addr_t aligned_sector; VasEBoot_dprintf ("disk", "Writing `%s'...\n", disk->name); if (VasEBoot_disk_adjust_range (disk, §or, &offset, size) != VAS_EBOOT_ERR_NONE) return -1; aligned_sector = (sector & ~((1ULL << (disk->log_sector_size - VAS_EBOOT_DISK_SECTOR_BITS)) - 1)); real_offset = offset + ((sector - aligned_sector) << VAS_EBOOT_DISK_SECTOR_BITS); sector = aligned_sector; while (size) { if (real_offset != 0 || (size < (1U << disk->log_sector_size) && size != 0)) { char *tmp_buf; VasEBoot_size_t len; VasEBoot_partition_t part; tmp_buf = VasEBoot_malloc (1U << disk->log_sector_size); if (!tmp_buf) return VasEBoot_errno; part = disk->partition; disk->partition = 0; if (VasEBoot_disk_read (disk, sector, 0, (1U << disk->log_sector_size), tmp_buf) != VAS_EBOOT_ERR_NONE) { disk->partition = part; VasEBoot_free (tmp_buf); goto finish; } disk->partition = part; len = (1U << disk->log_sector_size) - real_offset; if (len > size) len = size; VasEBoot_memcpy (tmp_buf + real_offset, buf, len); VasEBoot_disk_cache_invalidate (disk->dev->id, disk->id, sector); if ((disk->dev->disk_write) (disk, VasEBoot_disk_to_native_sector (disk, sector), 1, tmp_buf) != VAS_EBOOT_ERR_NONE) { VasEBoot_free (tmp_buf); goto finish; } VasEBoot_free (tmp_buf); sector += (1U << (disk->log_sector_size - VAS_EBOOT_DISK_SECTOR_BITS)); buf = (const char *) buf + len; size -= len; real_offset = 0; } else { VasEBoot_size_t len; VasEBoot_size_t n; len = size & ~((1ULL << disk->log_sector_size) - 1); n = size >> disk->log_sector_size; if (n > (disk->max_agglomerate << (VAS_EBOOT_DISK_CACHE_BITS + VAS_EBOOT_DISK_SECTOR_BITS - disk->log_sector_size))) n = (disk->max_agglomerate << (VAS_EBOOT_DISK_CACHE_BITS + VAS_EBOOT_DISK_SECTOR_BITS - disk->log_sector_size)); if ((disk->dev->disk_write) (disk, VasEBoot_disk_to_native_sector (disk, sector), n, buf) != VAS_EBOOT_ERR_NONE) goto finish; while (n--) { VasEBoot_disk_cache_invalidate (disk->dev->id, disk->id, sector); sector += (1U << (disk->log_sector_size - VAS_EBOOT_DISK_SECTOR_BITS)); } buf = (const char *) buf + len; size -= len; } } finish: return VasEBoot_errno; } VAS_EBOOT_MOD_INIT(disk) { VasEBoot_disk_write_weak = VasEBoot_disk_write; } VAS_EBOOT_MOD_FINI(disk) { VasEBoot_disk_write_weak = NULL; }