paul@272 | 1 | /* |
paul@272 | 2 | * Access to memory cards. |
paul@272 | 3 | * |
paul@272 | 4 | * Copyright (C) 2023, 2024 Paul Boddie <paul@boddie.org.uk> |
paul@272 | 5 | * |
paul@272 | 6 | * This program is free software; you can redistribute it and/or |
paul@272 | 7 | * modify it under the terms of the GNU General Public License as |
paul@272 | 8 | * published by the Free Software Foundation; either version 2 of |
paul@272 | 9 | * the License, or (at your option) any later version. |
paul@272 | 10 | * |
paul@272 | 11 | * This program is distributed in the hope that it will be useful, |
paul@272 | 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
paul@272 | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
paul@272 | 14 | * GNU General Public License for more details. |
paul@272 | 15 | * |
paul@272 | 16 | * You should have received a copy of the GNU General Public License |
paul@272 | 17 | * along with this program; if not, write to the Free Software |
paul@272 | 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
paul@272 | 19 | * Boston, MA 02110-1301, USA |
paul@272 | 20 | */ |
paul@272 | 21 | |
paul@272 | 22 | #include <l4/devices/msc.h> |
paul@272 | 23 | #include <l4/sys/cache.h> |
paul@272 | 24 | |
paul@272 | 25 | #include "msc_region_operations.h" |
paul@272 | 26 | |
paul@272 | 27 | |
paul@272 | 28 | |
paul@272 | 29 | MscRegionOperations::MscRegionOperations(Msc_channel *msc_channel, uint8_t card, |
paul@272 | 30 | struct dma_region region) |
paul@272 | 31 | : _msc_channel(msc_channel), _card(card), _region(region) |
paul@272 | 32 | { |
paul@272 | 33 | } |
paul@272 | 34 | |
paul@272 | 35 | long MscRegionOperations::read_partition_table(struct partition_table_entry **entry) |
paul@272 | 36 | { |
paul@272 | 37 | l4_cache_inv_data(_region.vaddr, _region.vaddr + _region.size); |
paul@272 | 38 | |
paul@272 | 39 | uint32_t transferred = _msc_channel->read(_card, &_region, 0, _region.size); |
paul@272 | 40 | |
paul@272 | 41 | if (!transferred) |
paul@272 | 42 | return -L4_EIO; |
paul@272 | 43 | |
paul@272 | 44 | *entry = get_partition_table((uint8_t *) _region.vaddr); |
paul@272 | 45 | |
paul@272 | 46 | if (*entry == NULL) |
paul@272 | 47 | return -L4_EIO; |
paul@272 | 48 | |
paul@272 | 49 | return L4_EOK; |
paul@272 | 50 | } |
paul@272 | 51 | |
paul@272 | 52 | /* Public operations. */ |
paul@272 | 53 | |
paul@272 | 54 | long MscRegionOperations::get_partition(uint8_t partition, |
paul@272 | 55 | uint32_t *start_block, |
paul@272 | 56 | uint32_t *num_blocks) |
paul@272 | 57 | { |
paul@272 | 58 | std::lock_guard<std::mutex> guard(_lock); |
paul@272 | 59 | |
paul@272 | 60 | // Use the fileid to select the partition. |
paul@272 | 61 | |
paul@272 | 62 | if (partition >= 4) |
paul@272 | 63 | return -L4_ENOENT; |
paul@272 | 64 | |
paul@272 | 65 | struct partition_table_entry *entry; |
paul@272 | 66 | long err = read_partition_table(&entry); |
paul@272 | 67 | |
paul@272 | 68 | if (err) |
paul@272 | 69 | return err; |
paul@272 | 70 | |
paul@272 | 71 | if (!entry[partition].num_sectors) |
paul@272 | 72 | return -L4_ENOENT; |
paul@272 | 73 | |
paul@272 | 74 | *start_block = entry[partition].lba_first_sector; |
paul@272 | 75 | *num_blocks = entry[partition].num_sectors; |
paul@272 | 76 | |
paul@272 | 77 | return L4_EOK; |
paul@272 | 78 | } |
paul@272 | 79 | |
paul@272 | 80 | void MscRegionOperations::read(l4_addr_t vaddr, l4re_dma_space_dma_addr_t paddr, |
paul@272 | 81 | offset_t filepos, offset_t size) |
paul@272 | 82 | { |
paul@272 | 83 | std::lock_guard<std::mutex> guard(_lock); |
paul@272 | 84 | |
paul@272 | 85 | struct dma_region region; |
paul@272 | 86 | |
paul@272 | 87 | // NOTE: Truncating the file position and size. |
paul@272 | 88 | |
paul@272 | 89 | region.vaddr = vaddr; |
paul@272 | 90 | region.paddr = paddr; |
paul@272 | 91 | region.size = size; |
paul@272 | 92 | |
paul@272 | 93 | l4_cache_inv_data(vaddr, vaddr + size); |
paul@272 | 94 | |
paul@272 | 95 | _msc_channel->read(_card, ®ion, (uint32_t) filepos, (uint32_t) size); |
paul@272 | 96 | } |
paul@272 | 97 | |
paul@272 | 98 | void MscRegionOperations::write(l4_addr_t vaddr, l4re_dma_space_dma_addr_t paddr, |
paul@272 | 99 | offset_t filepos, offset_t size) |
paul@272 | 100 | { |
paul@272 | 101 | std::lock_guard<std::mutex> guard(_lock); |
paul@272 | 102 | |
paul@272 | 103 | // NOTE: To be implemented. |
paul@272 | 104 | |
paul@272 | 105 | (void) vaddr; (void) paddr; (void) filepos; (void) size; |
paul@272 | 106 | } |