#include <stdint.h>
#include <stddef.h>
#include <limine.h>
#include "baseimg.h"
#include "bootinfo/bootinfo.h"
#include "kprintf.h"
#include "util/util.h"
#include "hal/hal.h"

struct limine_file *baseimg = NULL;

uint64_t baseimg_getaddr(void) {
  return (uint64_t)baseimg->address;
}

uint64_t baseimg_getsize(void) {
  return baseimg->size;
}

void baseimg_init(void) {
  LOG("baseimg", "looking for base image...\n");
  for (size_t i = 0; i < BOOT_INFO.modules->module_count; i++) {
    struct limine_file *module = BOOT_INFO.modules->modules[i];
    if (hal_strcmp(util_get_filename(module->path), "base.img") == 0) {
      baseimg = module;
      break;
    }
  }

  if (baseimg == NULL) {
    ERR("baseimg", "base.img not found\n");
    hal_hang();
  } else {
    LOG("baseimg", "base.img found\n");
    LOG("baseimg", "addr = %p, size = %lu\n", baseimg->address, baseimg->size);
    for (size_t i = 0; i < 30; i++) {
      kprintf("%02X ", ((uint8_t *)(baseimg->address))[i]);
      if (i > 0 && (i + 1) % 10 == 0) {
        kprintf("\n");
      }
    }
  }
}
