Home | History | Annotate | Download | only in stub
      1 /* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
      2  * Use of this source code is governed by a BSD-style license that can be
      3  * found in the LICENSE file.
      4  *
      5  * Stub implementations of stream APIs.
      6  */
      7 
      8 #include <stdint.h>
      9 
     10 #define _STUB_IMPLEMENTATION_
     11 
     12 #include "vboot_api.h"
     13 
     14 /* The stub implementation assumes 512-byte disk sectors */
     15 #define LBA_BYTES 512
     16 
     17 /* Internal struct to simulate a stream for sector-based disks */
     18 struct disk_stream {
     19 	/* Disk handle */
     20 	VbExDiskHandle_t handle;
     21 
     22 	/* Next sector to read */
     23 	uint64_t sector;
     24 
     25 	/* Number of sectors left in partition */
     26 	uint64_t sectors_left;
     27 };
     28 
     29 VbError_t VbExStreamOpen(VbExDiskHandle_t handle, uint64_t lba_start,
     30 			 uint64_t lba_count, VbExStream_t *stream)
     31 {
     32 	struct disk_stream *s;
     33 
     34 	if (!handle) {
     35 		*stream = NULL;
     36 		return VBERROR_UNKNOWN;
     37 	}
     38 
     39 	s = VbExMalloc(sizeof(*s));
     40 	s->handle = handle;
     41 	s->sector = lba_start;
     42 	s->sectors_left = lba_count;
     43 
     44 	*stream = (void *)s;
     45 
     46 	return VBERROR_SUCCESS;
     47 }
     48 
     49 VbError_t VbExStreamRead(VbExStream_t stream, uint32_t bytes, void *buffer)
     50 {
     51 	struct disk_stream *s = (struct disk_stream *)stream;
     52 	uint64_t sectors;
     53 	VbError_t rv;
     54 
     55 	if (!s)
     56 		return VBERROR_UNKNOWN;
     57 
     58 	/* For now, require reads to be a multiple of the LBA size */
     59 	if (bytes % LBA_BYTES)
     60 		return VBERROR_UNKNOWN;
     61 
     62 	/* Fail on overflow */
     63 	sectors = bytes / LBA_BYTES;
     64 	if (sectors > s->sectors_left)
     65 		return VBERROR_UNKNOWN;
     66 
     67 	rv = VbExDiskRead(s->handle, s->sector, sectors, buffer);
     68 	if (rv != VBERROR_SUCCESS)
     69 		return rv;
     70 
     71 	s->sector += sectors;
     72 	s->sectors_left -= sectors;
     73 
     74 	return VBERROR_SUCCESS;
     75 }
     76 
     77 void VbExStreamClose(VbExStream_t stream)
     78 {
     79 	struct disk_stream *s = (struct disk_stream *)stream;
     80 
     81 	/* Allow freeing a null pointer */
     82 	if (!s)
     83 		return;
     84 
     85 	VbExFree(s);
     86 	return;
     87 }
     88