<commit_before>#include <efi.h>
#include <efilib.h>
#include "acpi.h"
#include "multiboot2_util.h"

static acpi1_rsdp_t *acpi1_rsdp = NULL;
static acpi2_rsdp_t *acpi2_rsdp = NULL;
static efi_mmap_t efi_mmap;
static INTN e820_map_overflow = 0;
static unsigned int e820_count = 0;
UINT8 g_e820_mmap[2560];
extern EFI_GUID GraphicsOutputProtocol;


CHAR8 *
char16_to_char8 (IN CHAR16 * src, OUT CHAR8 * dest, IN UINT16 size) {

  int i;
  for (i = 0; i < size; i++)
    dest[i] = (CHAR8) src[i];
  return dest;
}

void
print_msg (char *file, int line, char *msg, char type, ...) {

  va_list arg_p;
  va_start (arg_p, type);

  /*
     a       -   ascii string
     s       -   unicode string
     X       -   fixed 8 byte value in hex
     x       -   hex value
     d       -   value as decimal
     c       -   Unicode char
     t       -   EFI time structure
     g       -   Pointer to GUID
     r       -   EFI status code (result code)
     o    -   default(or any other char)
   */

  switch (type) {
  case 'a':{
      CHAR8 *a = va_arg (arg_p, CHAR8 *);
      Print (L"%a: %d : %a : %a\n", file, line, msg, a);
    }
    break;
  case 's':{
      CHAR16 *s = va_arg (arg_p, CHAR16 *);
      Print (L"%a: %d : %a : %s\n", file, line, msg, s);
    }
    break;
  case 'X':{
      UINT64 X = va_arg (arg_p, UINT64);
      Print (L"%a: %d : %a : %X\n", file, line, msg, X);
    }
    break;
  case 'x':{
      UINT32 x = va_arg (arg_p, UINT32);
      Print (L"%a: %d : %a : %x\n", file, line, msg, x);
    }
    break;
  case 'd':{
      UINT32 d = va_arg (arg_p, UINT32);
      Print (L"%a: %d : %a : %d\n", file, line, msg, d);
    }
    break;
  case 'c':{
      UINTN c = va_arg (arg_p, UINTN);
      Print (L"%a: %d : %a : %c\n", file, line, msg, c);
    }
    break;
  case 't':{
      EFI_TIME *t = va_arg (arg_p, EFI_TIME *);
      Print (L"%a: %d : %a : %t\n", file, line, msg, t);
    }
    break;
  case 'g':{
      EFI_GUID *g = va_arg (arg_p, EFI_GUID *);
      Print (L"%a: %d : %a : %g\n", file, line, msg, g);
    }
    break;
  case 'r':{
      EFI_STATUS r = va_arg (arg_p, EFI_STATUS);
      Print (L"%a: %d : %a : %r\n", file, line, msg, r);
    }
    break;
  default:
    Print (L"%a: %d : %a \n", file, line, msg);
    break;

  }
  va_end (arg_p);
  uefi_call_wrapper (BS->Stall, 1, 3 * 1000 * 1000);
}

EFI_STATUS
copy_file_buf (EFI_HANDLE parent_image, CHAR16 * file, CHAR8 ** buf,
	       UINTN * buf_len) {
  EFI_STATUS err;
  EFI_LOADED_IMAGE *loaded_image;
  EFI_FILE_HANDLE root_dir;
  EFI_FILE_HANDLE file_handle;
  UINTN tmp_sz;
  EFI_FILE_INFO tmp_buf;

  err =
    uefi_call_wrapper (BS->OpenProtocol, 6, parent_image,
		       &LoadedImageProtocol, (void **) &loaded_image,
		       parent_image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);

  if (EFI_ERROR (err)) {
    print_msg ("multiboot2.c", __LINE__, ERR_LOAD_IMG_HDL, 'r', err);
    return err;
  }

  root_dir = LibOpenRoot (loaded_image->DeviceHandle);
  if (!root_dir) {
    print_msg ("multiboot2.c", __LINE__, ERR_ROOT_DIR, 'r', EFI_LOAD_ERROR);
    return EFI_LOAD_ERROR;
  }

  err =
    uefi_call_wrapper (root_dir->Open, 5, root_dir, &file_handle, file,
		       EFI_FILE_MODE_READ, 0ULL);
  if (EFI_ERROR (err)) {
    print_msg ("multiboot2.c", __LINE__, ERR_OPEN_FILE, 'r', err);
    return err;
  }

  /*read the whole file */
  if (*buf_len == 0) {

    tmp_sz = SIZE_OF_EFI_FILE_INFO + (StrLen (file) * sizeof (CHAR16));
    err =
      uefi_call_wrapper (file_handle->GetInfo, 4, file_handle,
			 &GenericFileInfo, &tmp_sz, &tmp_buf);

    if (EFI_ERROR (err)) {
      print_msg ("multiboot2.c", __LINE__, ERR_GET_FILE_SZ, 'r', err);
      uefi_call_wrapper (file_handle->Close, 1, file_handle);
      return EFI_LOAD_ERROR;
    }

    *buf_len = tmp_buf.FileSize;
  }

  *buf = AllocateZeroPool (*buf_len);
  err = uefi_call_wrapper (file_handle->Read, 3, file_handle, buf_len, *buf);


  if (EFI_ERROR (err) || *buf_len < 32) {
    print_msg ("multiboot2.c", __LINE__, ERR_READ_FILE, 'r', err);
    uefi_call_wrapper (file_handle->Close, 1, file_handle);
    return EFI_LOAD_ERROR;
  }

  uefi_call_wrapper (file_handle->Close, 1, file_handle);
  return EFI_SUCCESS;
}

EFI_STATUS
parse_header (CHAR8 * buf, UINTN len) {
  bool has_entry_addr_tag = false;

  mboot_hdr_p hdr;
  mboot_hdr_tag_p tag;
  mboot_hdr_tag_addr_p addr_tag = NULL;<commit_msg>tummiboot: fix long file names bug in copy_file_buf<commit_after>#include <efi.h>
#include <efilib.h>
#include "acpi.h"
#include "multiboot2_util.h"

static acpi1_rsdp_t *acpi1_rsdp = NULL;
static acpi2_rsdp_t *acpi2_rsdp = NULL;
static efi_mmap_t efi_mmap;
static INTN e820_map_overflow = 0;
static unsigned int e820_count = 0;
UINT8 g_e820_mmap[2560];
extern EFI_GUID GraphicsOutputProtocol;


CHAR8 *
char16_to_char8 (IN CHAR16 * src, OUT CHAR8 * dest, IN UINT16 size) {

  int i;
  for (i = 0; i < size; i++)
    dest[i] = (CHAR8) src[i];
  return dest;
}

void
print_msg (char *file, int line, char *msg, char type, ...) {

  va_list arg_p;
  va_start (arg_p, type);

  /*
     a       -   ascii string
     s       -   unicode string
     X       -   fixed 8 byte value in hex
     x       -   hex value
     d       -   value as decimal
     c       -   Unicode char
     t       -   EFI time structure
     g       -   Pointer to GUID
     r       -   EFI status code (result code)
     o    -   default(or any other char)
   */

  switch (type) {
  case 'a':{
      CHAR8 *a = va_arg (arg_p, CHAR8 *);
      Print (L"%a: %d : %a : %a\n", file, line, msg, a);
    }
    break;
  case 's':{
      CHAR16 *s = va_arg (arg_p, CHAR16 *);
      Print (L"%a: %d : %a : %s\n", file, line, msg, s);
    }
    break;
  case 'X':{
      UINT64 X = va_arg (arg_p, UINT64);
      Print (L"%a: %d : %a : %X\n", file, line, msg, X);
    }
    break;
  case 'x':{
      UINT32 x = va_arg (arg_p, UINT32);
      Print (L"%a: %d : %a : %x\n", file, line, msg, x);
    }
    break;
  case 'd':{
      UINT32 d = va_arg (arg_p, UINT32);
      Print (L"%a: %d : %a : %d\n", file, line, msg, d);
    }
    break;
  case 'c':{
      UINTN c = va_arg (arg_p, UINTN);
      Print (L"%a: %d : %a : %c\n", file, line, msg, c);
    }
    break;
  case 't':{
      EFI_TIME *t = va_arg (arg_p, EFI_TIME *);
      Print (L"%a: %d : %a : %t\n", file, line, msg, t);
    }
    break;
  case 'g':{
      EFI_GUID *g = va_arg (arg_p, EFI_GUID *);
      Print (L"%a: %d : %a : %g\n", file, line, msg, g);
    }
    break;
  case 'r':{
      EFI_STATUS r = va_arg (arg_p, EFI_STATUS);
      Print (L"%a: %d : %a : %r\n", file, line, msg, r);
    }
    break;
  default:
    Print (L"%a: %d : %a \n", file, line, msg);
    break;

  }
  va_end (arg_p);
  uefi_call_wrapper (BS->Stall, 1, 3 * 1000 * 1000);
}

EFI_STATUS
copy_file_buf (EFI_HANDLE parent_image, CHAR16 * file, CHAR8 ** buf,
	       UINTN * buf_len) {
  EFI_STATUS err;
  EFI_LOADED_IMAGE *loaded_image;
  EFI_FILE_HANDLE root_dir;
  EFI_FILE_HANDLE file_handle;
  UINTN tmp_sz;
  VOID *tmp_buf;

  err =
    uefi_call_wrapper (BS->OpenProtocol, 6, parent_image,
		       &LoadedImageProtocol, (void **) &loaded_image,
		       parent_image, NULL, EFI_OPEN_PROTOCOL_GET_PROTOCOL);

  if (EFI_ERROR (err)) {
    print_msg ("multiboot2.c", __LINE__, ERR_LOAD_IMG_HDL, 'r', err);
    return err;
  }

  root_dir = LibOpenRoot (loaded_image->DeviceHandle);
  if (!root_dir) {
    print_msg ("multiboot2.c", __LINE__, ERR_ROOT_DIR, 'r', EFI_LOAD_ERROR);
    return EFI_LOAD_ERROR;
  }

  err =
    uefi_call_wrapper (root_dir->Open, 5, root_dir, &file_handle, file,
		       EFI_FILE_MODE_READ, 0ULL);
  if (EFI_ERROR (err)) {
    print_msg ("multiboot2.c", __LINE__, ERR_OPEN_FILE, 'r', err);
    return err;
  }

  /*read the whole file */
  if (*buf_len == 0) {

    tmp_sz = SIZE_OF_EFI_FILE_INFO + (StrLen (file) * sizeof (CHAR16));
    tmp_buf = AllocateZeroPool (tmp_sz);
    err = uefi_call_wrapper (file_handle->GetInfo, 4, file_handle,
      &GenericFileInfo, &tmp_sz, tmp_buf);

    if (EFI_ERROR (err)) {
      print_msg ("multiboot2.c", __LINE__, ERR_GET_FILE_SZ, 'r', err);
      uefi_call_wrapper (file_handle->Close, 1, file_handle);
      return EFI_LOAD_ERROR;
    }

    *buf_len = ((EFI_FILE_INFO *)tmp_buf)->FileSize;
  }

  *buf = AllocateZeroPool (*buf_len);
  err = uefi_call_wrapper (file_handle->Read, 3, file_handle, buf_len, *buf);


  if (EFI_ERROR (err) || *buf_len < 32) {
    print_msg ("multiboot2.c", __LINE__, ERR_READ_FILE, 'r', err);
    uefi_call_wrapper (file_handle->Close, 1, file_handle);
    return EFI_LOAD_ERROR;
  }

  uefi_call_wrapper (file_handle->Close, 1, file_handle);
  return EFI_SUCCESS;
}

EFI_STATUS
parse_header (CHAR8 * buf, UINTN len) {
  bool has_entry_addr_tag = false;

  mboot_hdr_p hdr;
  mboot_hdr_tag_p tag;
  mboot_hdr_tag_addr_p addr_tag = NULL;
{'fraction_non_alphanumeric': 0.14482053611994547, 'fraction_numerical': 0.018855065879145844, 'mean_word_length': 2.7325137770241628, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4909973', 'n_tokens_mistral': 3808, 'n_tokens_neox': 3631, 'n_words': 912}