aboutsummaryrefslogtreecommitdiffstats
path: root/bootloader/disk.c
blob: 646448ea99f989aa26bf0f546996200304ccbaec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <efi.h>
#include <efilib.h>

#include "disk.h"
#include "memory.h"

UINTN EFIAPI ceil2(float x)
{
  UINTN ret = x;
  if (x - (int)x > 0) {
      x += 1;
      ret = (int)x;
  }

  return ret;
}

void * EFIAPI LoadFile(CHAR16 * name, UINTN memtype, int32_t* filesize)
{
  // ask for the protocol to handle the filesystem
  EFI_GUID EfiSimpleFileSystemGuid = SIMPLE_FILE_SYSTEM_PROTOCOL;
  EFI_FILE_IO_INTERFACE * FSInterface = NULL;
  struct _EFI_FILE_HANDLE * fsroot = NULL;

  EFI_STATUS localteprotocolstat = uefi_call_wrapper(BS->LocateProtocol, 3, &EfiSimpleFileSystemGuid, NULL, &FSInterface);

  if(localteprotocolstat != EFI_SUCCESS)
  {
    Print(L"Failed to locate protocol\n");
  }

  EFI_STATUS openvolumestat = uefi_call_wrapper(FSInterface->OpenVolume, 2, FSInterface, &fsroot);

  if(openvolumestat != EFI_SUCCESS)
  {
    Print(L"FAiled to open the volume\n");
  }

  struct _EFI_FILE_HANDLE * fp = NULL;

  EFI_STATUS openstat = uefi_call_wrapper(fsroot->Open, 5, fsroot, &fp, name, (UINT64)1, (UINT64)1);



  if(openstat != EFI_SUCCESS)
  {
    Print(L"Failed opening the file\n");
  }


  // even if the specification says that asking to open the file with insuficient memory
  // reports the full file size, qemu and virtualbox implementations don't do that
  // as such we need to get manually the file info
  UINTN infosize = 1024;
  EFI_FILE_INFO * info = AllocatePool(infosize);
  EFI_GUID infoguid = EFI_FILE_INFO_ID;

  EFI_STATUS infostatus = uefi_call_wrapper(fp->GetInfo, 4, fp, &infoguid, &infosize, info);

  if(infostatus != EFI_SUCCESS)
  {
    Print(L"Failed to get FileInfo\n");
  }

  *filesize = info->FileSize;

  UINTN size = info->FileSize;
  void * data = NULL;

  //data = AllocatePool(size);
  //EFI_STATUS allocstatus = uefi_call_wrapper(BS->AllocatePool, 3, EfiLoaderData, size, (void**)&data);
  float pages = (size/1024)/4;

  UINTN ipages = ceil2(pages);
  EFI_PHYSICAL_ADDRESS tmp;

  EFI_STATUS allocstatus = uefi_call_wrapper(BS->AllocatePages, 4, AllocateAnyPages, memtype, ipages, &tmp);

  data = (void*)tmp;

  //data = AllocatePages2(size);

  //if(data == NULL)
  if(allocstatus != EFI_SUCCESS)
  {
    Print(L"Failed to get Kernel memory\n");
  }

  // phoenix lies, must specify the size myself
  uefi_call_wrapper(fp->Read, 3, fp, &size, data);

  // Print(L"Read Size: %d\n", size);

  uefi_call_wrapper(fp->Close, 1, fp);

  return data;
}