| /* |
| * Copyright 2016 Google Inc. |
| * |
| * See file CREDITS for list of people who contributed to this |
| * project. |
| * |
| * This program 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 2 of |
| * the License, or (at your option) any later version. |
| * |
| * This program 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 this program; if not, write to the Free Software |
| * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| * MA 02111-1307 USA |
| */ |
| |
| #include <stdio.h> |
| #include <string.h> |
| |
| #include "base/cbfs/cbfs.h" |
| #include "base/container_of.h" |
| #include "base/xalloc.h" |
| #include "drivers/storage/cbfs.h" |
| |
| static int cbfs_storage_init(CbfsStorage *storage) |
| { |
| storage->data = cbfs_get_file_content( |
| storage->media, storage->name, storage->type, |
| &storage->size); |
| if (!storage->data) |
| return 1; |
| else |
| return 0; |
| } |
| |
| static int cbfs_storage_read(StorageOps *me, void *buffer, |
| uint64_t offset, size_t size) |
| { |
| CbfsStorage *storage = container_of(me, CbfsStorage, ops); |
| |
| if (!storage->data && cbfs_storage_init(storage)) |
| return 1; |
| |
| if (offset + size > storage->size || offset + size < offset) { |
| printf("Read beyond the bounds of CBFS data.\n"); |
| return 1; |
| } |
| |
| memcpy(buffer, (uint8_t *)storage->data + offset, size); |
| return 0; |
| } |
| |
| static int cbfs_storage_write(StorageOps *me, const void *buffer, |
| uint64_t offset, size_t size) |
| { |
| printf("CBFS storage is read only.\n"); |
| return 1; |
| } |
| |
| static int cbfs_storage_size(StorageOps *me) |
| { |
| CbfsStorage *storage = container_of(me, CbfsStorage, ops); |
| |
| if (!storage->data && cbfs_storage_init(storage)) |
| return 1; |
| |
| return storage->size; |
| } |
| |
| CbfsStorage *new_cbfs_storage(struct cbfs_media *media, const char *name, |
| int type) |
| { |
| CbfsStorage *storage = xzalloc(sizeof(*storage)); |
| |
| storage->ops.read = &cbfs_storage_read; |
| storage->ops.write = &cbfs_storage_write; |
| storage->ops.size = &cbfs_storage_size; |
| |
| storage->media = media; |
| storage->name = name; |
| storage->type = type; |
| |
| return storage; |
| } |