Challenge of handmade OS
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

56 lines
1.8 KiB

#include "ramfs.h"
#include <Guid/FileInfo.h>
#include <Library/BaseLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/PrintLib.h>
#include <Library/UefiLib.h>
#include <Uefi.h>
#include "text.h"
RAMFS_DATA *data;
void ramfsinit(
EFI_SYSTEM_TABLE *SystemTable) { // must call before ExitBootServices
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *fsp;
EFI_FILE_PROTOCOL *Root, *ramfs;
EFI_FILE_INFO info;
UINTN infosize = sizeof(info), datasize;
EFI_STATUS Status = SystemTable->BootServices->LocateProtocol(
&gEfiSimpleFileSystemProtocolGuid, NULL, (VOID **)&fsp);
if (EFI_ERROR(Status)) {
Print(L"Error:Failed to locate EFI Simple FileSystem Protocol - %r",
Status);
return;
}
fsp->OpenVolume(fsp, &Root);
Status = Root->Open(Root, &ramfs, L"ramfs.img", EFI_FILE_MODE_READ, 0);
if (EFI_ERROR(Status)) {
Print(L"Error:Failed to open ramfs file - %r\n", Status);
return;
}
ramfs->GetInfo(ramfs, &gEfiFileInfoGuid, &infosize, &info);
Status = SystemTable->BootServices->AllocatePool(
EfiLoaderData, info.FileSize, (void **)&data);
if (EFI_ERROR(Status)) {
Print(L"Error:Failed to allocate ramfs memory - %r\n", Status);
return;
}
datasize = info.FileSize;
ramfs->Read(ramfs, &datasize, data);
}
void getfilesize(char *filename, UINTN *size) {
int index;
for (index = 0; index < 256; index++)
if (!AsciiStrCmp(data->descriptor[index].name, filename)) break;
*size = data->descriptor[index].Size;
}
void readfile(char *filename, UINTN buffersize, void *buffer) {
int index;
for (index = 0; index < 256; index++)
if (!AsciiStrCmp(data->descriptor[index].name, filename)) break;
CopyMem(buffer, data->data + data->descriptor[index].Start, buffersize);
}