1 // Copyright 2016 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include <efi/system-table.h>
6 #include <efi/types.h>
7 #include <efi/protocol/loaded-image.h>
8 #include <efi/protocol/simple-file-system.h>
9 
10 #include <stdio.h>
11 #include <xefi.h>
12 
efi_main(efi_handle img,efi_system_table * sys)13 EFIAPI efi_status efi_main(efi_handle img, efi_system_table* sys) {
14     efi_loaded_image_protocol* loaded;
15     efi_status r;
16 
17     xefi_init(img, sys);
18 
19     printf("Hello, EFI World\n");
20 
21     r = xefi_open_protocol(img, &LoadedImageProtocol, (void**)&loaded);
22     if (r)
23         xefi_fatal("LoadedImageProtocol", r);
24 
25     printf("Img DeviceHandle='%ls'\n", xefi_handle_to_str(loaded->DeviceHandle));
26     printf("Img FilePath='%ls'\n", xefi_devpath_to_str(loaded->FilePath));
27     printf("Img Base=%p Size=%lx\n", loaded->ImageBase, loaded->ImageSize);
28 
29     efi_simple_file_system_protocol* sfs;
30     r = xefi_open_protocol(loaded->DeviceHandle, &SimpleFileSystemProtocol, (void**)&sfs);
31     if (r)
32         xefi_fatal("SimpleFileSystemProtocol", r);
33 
34     efi_file_protocol* root;
35     r = sfs->OpenVolume(sfs, &root);
36     if (r)
37         xefi_fatal("OpenVolume", r);
38 
39     efi_file_protocol* file;
40     r = root->Open(root, &file, L"README.txt", EFI_FILE_MODE_READ, 0);
41 
42     if (r == EFI_SUCCESS) {
43         char buf[512];
44         size_t sz = sizeof(buf);
45         efi_file_info* finfo = (void*)buf;
46         r = file->GetInfo(file, &FileInfoGuid, &sz, finfo);
47         if (r)
48             xefi_fatal("GetInfo", r);
49         printf("FileSize %ld\n", finfo->FileSize);
50 
51         sz = sizeof(buf) - 1;
52         r = file->Read(file, &sz, buf);
53         if (r)
54             xefi_fatal("Read", r);
55 
56         char* x = buf;
57         while (sz-- > 0)
58             printf("%c", *x++);
59 
60         file->Close(file);
61     }
62 
63     root->Close(root);
64     xefi_close_protocol(loaded->DeviceHandle, &SimpleFileSystemProtocol);
65     xefi_close_protocol(img, &LoadedImageProtocol);
66 
67     xefi_wait_any_key();
68 
69     return EFI_SUCCESS;
70 }
71