vboot_api_stub_stream.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #include <stdint.h>
  8. #include "vboot_api.h"
  9. /* The stub implementation assumes 512-byte disk sectors */
  10. #define LBA_BYTES 512
  11. /* Internal struct to simulate a stream for sector-based disks */
  12. struct disk_stream {
  13. /* Disk handle */
  14. VbExDiskHandle_t handle;
  15. /* Next sector to read */
  16. uint64_t sector;
  17. /* Number of sectors left in partition */
  18. uint64_t sectors_left;
  19. };
  20. VbError_t VbExStreamOpen(VbExDiskHandle_t handle, uint64_t lba_start,
  21. uint64_t lba_count, VbExStream_t *stream)
  22. {
  23. struct disk_stream *s;
  24. if (!handle) {
  25. *stream = NULL;
  26. return VBERROR_UNKNOWN;
  27. }
  28. s = malloc(sizeof(*s));
  29. s->handle = handle;
  30. s->sector = lba_start;
  31. s->sectors_left = lba_count;
  32. *stream = (void *)s;
  33. return VBERROR_SUCCESS;
  34. }
  35. VbError_t VbExStreamRead(VbExStream_t stream, uint32_t bytes, void *buffer)
  36. {
  37. struct disk_stream *s = (struct disk_stream *)stream;
  38. uint64_t sectors;
  39. VbError_t rv;
  40. if (!s)
  41. return VBERROR_UNKNOWN;
  42. /* For now, require reads to be a multiple of the LBA size */
  43. if (bytes % LBA_BYTES)
  44. return VBERROR_UNKNOWN;
  45. /* Fail on overflow */
  46. sectors = bytes / LBA_BYTES;
  47. if (sectors > s->sectors_left)
  48. return VBERROR_UNKNOWN;
  49. rv = VbExDiskRead(s->handle, s->sector, sectors, buffer);
  50. if (rv != VBERROR_SUCCESS)
  51. return rv;
  52. s->sector += sectors;
  53. s->sectors_left -= sectors;
  54. return VBERROR_SUCCESS;
  55. }
  56. void VbExStreamClose(VbExStream_t stream)
  57. {
  58. struct disk_stream *s = (struct disk_stream *)stream;
  59. /* Allow freeing a null pointer */
  60. if (!s)
  61. return;
  62. free(s);
  63. return;
  64. }