#include <stdint.h>
#include <stddef.h>
#include "partsd.h"
#include "storedev.h"
#include "errors.h"
#include "kprintf.h"

int32_t partsd_init(struct StoreDev *sd, void *extra) {
  PartSdInitExtra *e = (PartSdInitExtra *)extra;
  sd->sd.partsd.sectorcount = e->sectorcount;
  sd->sd.partsd.startsector = e->startsector;
  sd->sd.partsd.parent = e->parent;
  sd->sectorsize = e->parent->sectorsize;
  return E_OK;
}

int32_t partsd_read(struct StoreDev *sd, uint8_t *const buffer, ptrdiff_t sector, ptrdiff_t off, size_t size) {
  PartSd *part = &sd->sd.partsd;
  return part->parent->read(part->parent, buffer, part->startsector + sector, off, size);
}

int32_t partsd_write(struct StoreDev *sd, const uint8_t *const buffer, ptrdiff_t sector, ptrdiff_t off, size_t size) {
  PartSd *part = &sd->sd.partsd;
  return part->parent->write(part->parent, buffer, part->startsector + sector, off, size);
}

int32_t partsd_cleanup(struct StoreDev *sd) {
  (void)sd;
  return E_OK;
}

size_t partsd_capacity(struct StoreDev *sd) {
  return sd->sd.partsd.parent->sectorsize * sd->sd.partsd.sectorcount;
}
