鱼C论坛

 找回密码
 立即注册
查看: 3586|回复: 19

[已解决]如何将.c文件在UEFI系统中编译并且运行?

[复制链接]
发表于 2022-10-13 15:17:49 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
还有就是.inf是什么意思?
最佳答案
2022-10-13 16:46:12
先从基础开始学吧
你现在的学识和你目前的需求还有很长一段距离
你不能先学乘除法然后再学加减法
你的先学会走,然后再学跑
所以,先从基础开始学吧
先学C语言
另外,学会百度问题
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-10-13 15:38:16 | 显示全部楼层
        UEFI 不是一种负责引导操作系统的协议吗,楼主玩的好高大上啊!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-13 15:45:59 | 显示全部楼层
jackz007 发表于 2022-10-13 15:38
UEFI 不是一种负责引导操作系统的协议吗,楼主玩的好高大上啊!

主要我是不太懂啊。。。所以问问有没有人知道给我讲讲。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-13 15:46:59 | 显示全部楼层
2wsx 发表于 2022-10-13 15:45
主要我是不太懂啊。。。所以问问有没有人知道给我讲讲。。。

        难道你在写属于自己的操作系统?
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-13 15:47:38 | 显示全部楼层
jackz007 发表于 2022-10-13 15:38
UEFI 不是一种负责引导操作系统的协议吗,楼主玩的好高大上啊!

怎么把.c文件变成.efi文件啊
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-13 16:05:20 | 显示全部楼层
2wsx 发表于 2022-10-13 15:47
怎么把.c文件变成.efi文件啊

       兄弟,折腾 .efi 文件可是不敢靠道听途说啊,小心你的小机机分分钟变成板砖,到那时,恐怕连哭鼻子都找不到地方啊。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-13 16:06:41 | 显示全部楼层
jackz007 发表于 2022-10-13 15:46
难道你在写属于自己的操作系统?

不是啊,我是找了一个别人的代码,想运行一下,需要在UEFI里编译运行。我现在不知道该咋编译。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-13 16:09:33 | 显示全部楼层
2wsx 发表于 2022-10-13 16:06
不是啊,我是找了一个别人的代码,想运行一下,需要在UEFI里编译运行。我现在不知道该咋编译。

         你说的 UEFI 居然是指一种 C 语言编译器?混了几十年江湖,头一回听说!
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-13 16:23:17 | 显示全部楼层
jackz007 发表于 2022-10-13 16:09
你说的 UEFI 居然是指一种 C 语言编译器?混了几十年江湖,头一回听说!

大佬 你了解SMBIOS吗?我目前是希望写代码读取SMBIOS的任意的Type,并将其Handle打印出来,读取任意一个的Type,打印出Type中的DeviceType、BusNum、DevFuncNum信息。根据读出来的Bus、Device、Function号读取相应PCI配置空间信息。所以我在四处找相关代码 想运行一下看看是什么效果
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-13 16:27:02 | 显示全部楼层
2wsx 发表于 2022-10-13 16:23
大佬 你了解SMBIOS吗?我目前是希望写代码读取SMBIOS的任意的Type,并将其Handle打印出来,读取任意一个 ...

          抱歉,你这个太专业了,恐怕这里没有人可以帮助到你,你应该去专业指向更加明确的论坛求助。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-13 16:46:12 | 显示全部楼层    本楼为最佳答案   
先从基础开始学吧
你现在的学识和你目前的需求还有很长一段距离
你不能先学乘除法然后再学加减法
你的先学会走,然后再学跑
所以,先从基础开始学吧
先学C语言
另外,学会百度问题
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-14 08:07:45 | 显示全部楼层
人造人 发表于 2022-10-13 16:46
先从基础开始学吧
你现在的学识和你目前的需求还有很长一段距离
你不能先学乘除法然后再学加减法

好的 谢谢啦
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-14 23:01:20 | 显示全部楼层
花了点时间研究了一下uefi编程
这个教程可以
https://kagurazakakotori.github.io/ubmp-cn/index.html

另外的参考资料
https://www.cnblogs.com/DirWang/ ... 03-uefi-hello-world
http://www.rodsbooks.com/efi-programming/index.html
https://uefi.org/sites/default/f ... Spec_2_10_Aug29.pdf

我现在正在看这个教程,准备研究图像输出部分
我现在缺一个printf函数,我去研究这个函数了
之后再帮你研究研究smbios的部分
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-14 23:12:46 | 显示全部楼层
jackz007 发表于 2022-10-13 16:09
你说的 UEFI 居然是指一种 C 语言编译器?混了几十年江湖,头一回听说!

他指的是这条指令
  1. x86_64-w64-mingw32-gcc -Wall -e efi_main -nostdlib -fno-builtin -Wl,--subsystem,10 -I /usr/include/efi -o main.efi main.c
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-14 23:22:26 | 显示全部楼层
人造人 发表于 2022-10-14 23:12
他指的是这条指令

        谢谢,我说嘛,原来他要找的是一条编译命令。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-19 01:00:33 | 显示全部楼层
printf函数过于复杂,我现在依然写不出来
不浪费时间了,这个函数之后再研究吧
我再研究研究efi编程,然后就帮你研究smbios

  1. #include <efi.h>

  2. void swap(CHAR16 *a, CHAR16 *b) {
  3.     CHAR16 temp = *a;
  4.     *a = *b;
  5.     *b = temp;
  6. }

  7. CHAR16 kgetc(EFI_SYSTEM_TABLE *SystemTable) {
  8.     EFI_INPUT_KEY key;
  9.     SystemTable->BootServices->WaitForEvent(1, &SystemTable->ConIn->WaitForKey, NULL);
  10.     SystemTable->ConIn->ReadKeyStroke(SystemTable->ConIn, &key);
  11.     if(key.UnicodeChar == '\r') key.UnicodeChar = '\n';
  12.     return key.UnicodeChar;
  13. }

  14. void kputc(EFI_SYSTEM_TABLE *SystemTable, CHAR16 c) {
  15.     if(c == L'\n') kputc(SystemTable, L'\r');
  16.     CHAR16 str[2] = {c, '\0'};
  17.     SystemTable->ConOut->OutputString(SystemTable->ConOut, str);
  18. }

  19. void kputs(EFI_SYSTEM_TABLE *SystemTable, const CHAR16 *str) {
  20.     for(UINTN i = 0; str[i]; ++i) {
  21.         kputc(SystemTable, str[i]);
  22.     }
  23. }

  24. void number(CHAR16 *buff, UINTN value, UINTN base) {
  25.     static const CHAR16 table[16] = L"0123456789abcdef";
  26.     if(base > 16) base = 16;
  27.     CHAR16 *p = buff;
  28.     do {
  29.         *p++ = table[value % base];
  30.         value /= base;
  31.     } while(value);
  32.     *p = L'\0';
  33.     UINTN count = p - buff;
  34.     for(UINTN i = 0; i < count / 2; ++i) {
  35.         swap(&buff[i], &buff[count - i - 1]);
  36.     }
  37. }

  38. void print_dec(EFI_SYSTEM_TABLE *SystemTable, UINTN value) {
  39.     CHAR16 buff[128]; number(buff, value, 10); kputs(SystemTable, buff); kputc(SystemTable, L'\n');
  40. }

  41. void print_hex(EFI_SYSTEM_TABLE *SystemTable, UINTN value) {
  42.     CHAR16 buff[128]; number(buff, value, 16); kputs(SystemTable, buff); kputc(SystemTable, L'\n');
  43. }

  44. void info(EFI_SYSTEM_TABLE *SystemTable, EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput) {
  45.     kputs(SystemTable, L"FrameBufferBase: "); print_hex(SystemTable, GraphicsOutput->Mode->FrameBufferBase);
  46.     kputs(SystemTable, L"FrameBufferSize: "); print_dec(SystemTable, GraphicsOutput->Mode->FrameBufferSize);
  47.     kputs(SystemTable, L"FrameBufferSize: "); print_hex(SystemTable, GraphicsOutput->Mode->FrameBufferSize);
  48.     kputs(SystemTable, L"MaxMode: "); print_dec(SystemTable, GraphicsOutput->Mode->MaxMode);
  49.     kputs(SystemTable, L"Mode: "); print_dec(SystemTable, GraphicsOutput->Mode->Mode);
  50.     kputs(SystemTable, L"SizeOfInfo: "); print_dec(SystemTable, GraphicsOutput->Mode->SizeOfInfo);
  51.     kputs(SystemTable, L"HorizontalResolution: "); print_dec(SystemTable, GraphicsOutput->Mode->Info->HorizontalResolution);
  52.     kputs(SystemTable, L"PixelsPerScanLine: "); print_dec(SystemTable, GraphicsOutput->Mode->Info->PixelsPerScanLine);
  53.     kputs(SystemTable, L"PixelFormat: "); print_dec(SystemTable, GraphicsOutput->Mode->Info->PixelFormat);
  54.     kputs(SystemTable, L"Version: "); print_dec(SystemTable, GraphicsOutput->Mode->Info->Version);
  55.     kputs(SystemTable, L"VerticalResolution: "); print_dec(SystemTable, GraphicsOutput->Mode->Info->VerticalResolution);
  56.     kputs(SystemTable, L"BlueMask: "); print_dec(SystemTable, GraphicsOutput->Mode->Info->PixelInformation.BlueMask);
  57.     kputs(SystemTable, L"GreenMask: "); print_dec(SystemTable, GraphicsOutput->Mode->Info->PixelInformation.GreenMask);
  58.     kputs(SystemTable, L"RedMask: "); print_dec(SystemTable, GraphicsOutput->Mode->Info->PixelInformation.RedMask);
  59.     kputs(SystemTable, L"ReservedMask: "); print_dec(SystemTable, GraphicsOutput->Mode->Info->PixelInformation.ReservedMask);
  60.     for(UINTN i = 0; i < GraphicsOutput->Mode->MaxMode; ++i) {
  61.         kputs(SystemTable, L"i: "); print_dec(SystemTable, i);
  62.         EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *GraphicsOutputMode;
  63.         UINTN SizeOfInfo;
  64.         GraphicsOutput->QueryMode(GraphicsOutput, i, &SizeOfInfo, &GraphicsOutputMode);
  65.         kputs(SystemTable, L"SizeOfInfo: "); print_dec(SystemTable, SizeOfInfo);
  66.         kputs(SystemTable, L"HorizontalResolution: "); print_dec(SystemTable, GraphicsOutputMode->HorizontalResolution);
  67.         kputs(SystemTable, L"PixelsPerScanLine: "); print_dec(SystemTable, GraphicsOutputMode->PixelsPerScanLine);
  68.         kputs(SystemTable, L"PixelFormat: "); print_dec(SystemTable, GraphicsOutputMode->PixelFormat);
  69.         kputs(SystemTable, L"Version: "); print_dec(SystemTable, GraphicsOutputMode->Version);
  70.         kputs(SystemTable, L"VerticalResolution: "); print_dec(SystemTable, GraphicsOutputMode->VerticalResolution);
  71.         kputs(SystemTable, L"BlueMask: "); print_dec(SystemTable, GraphicsOutputMode->PixelInformation.BlueMask);
  72.         kputs(SystemTable, L"GreenMask: "); print_dec(SystemTable, GraphicsOutputMode->PixelInformation.GreenMask);
  73.         kputs(SystemTable, L"RedMask: "); print_dec(SystemTable, GraphicsOutputMode->PixelInformation.RedMask);
  74.         kputs(SystemTable, L"ReservedMask: "); print_dec(SystemTable, GraphicsOutputMode->PixelInformation.ReservedMask);
  75.     }
  76. }

  77. void fill_screen(EFI_SYSTEM_TABLE *SystemTable, EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput) {
  78.     //GraphicsOutput->SetMode(GraphicsOutput, 22);    // 1920x1080
  79.     GraphicsOutput->SetMode(GraphicsOutput, 18);    // 1440x900
  80.     EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer = (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)GraphicsOutput->Mode->FrameBufferBase;
  81.     UINTN height = GraphicsOutput->Mode->Info->VerticalResolution;
  82.     UINTN width = GraphicsOutput->Mode->Info->HorizontalResolution;
  83.     EFI_GRAPHICS_OUTPUT_BLT_PIXEL color = {255, 255, 255};
  84.     for(UINTN y = 0; y < height; ++y) {
  85.         for(UINTN x = 0; x < width; ++x) {
  86.             BltBuffer[y * width + x] = color;
  87.         }
  88.     }
  89. }

  90. EFI_STATUS EFIAPI efi_main(EFI_HANDLE *ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
  91.     EFI_GUID gop_guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
  92.     EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput;
  93.     SystemTable->BootServices->LocateProtocol(&gop_guid, NULL, (VOID **)&GraphicsOutput);
  94.     info(SystemTable, GraphicsOutput);
  95.     fill_screen(SystemTable, GraphicsOutput);
  96.     kgetc(SystemTable);
  97.     return EFI_SUCCESS;
  98. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-19 08:03:46 | 显示全部楼层
人造人 发表于 2022-10-19 01:00
printf函数过于复杂,我现在依然写不出来
不浪费时间了,这个函数之后再研究吧
我再研究研究efi编程,然 ...

OK 感谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-22 00:03:29 | 显示全部楼层
好了,uefi编程的研究到此为止,接下来帮你研究smbios
下面是我目前的研究成果,有兴趣的话你可以看看

  1. $ cat main.c
  2. #include <efi.h>

  3. #define print_variable(SystemTable, base, value) \
  4.     kputs(SystemTable, L"" #value ": "); print_##base(SystemTable, value)

  5. void swap(CHAR16 *a, CHAR16 *b) {
  6.     CHAR16 temp = *a;
  7.     *a = *b;
  8.     *b = temp;
  9. }

  10. CHAR16 kgetc(EFI_SYSTEM_TABLE *SystemTable) {
  11.     EFI_INPUT_KEY key;
  12.     SystemTable->BootServices->WaitForEvent(1, &SystemTable->ConIn->WaitForKey, NULL);
  13.     SystemTable->ConIn->ReadKeyStroke(SystemTable->ConIn, &key);
  14.     if(key.UnicodeChar == '\r') key.UnicodeChar = '\n';
  15.     return key.UnicodeChar;
  16. }

  17. void kputc(EFI_SYSTEM_TABLE *SystemTable, CHAR16 c) {
  18.     if(c == L'\n') kputc(SystemTable, L'\r');
  19.     CHAR16 str[2] = {c, '\0'};
  20.     SystemTable->ConOut->OutputString(SystemTable->ConOut, str);
  21. }

  22. void kputs(EFI_SYSTEM_TABLE *SystemTable, const CHAR16 *str) {
  23.     for(UINTN i = 0; str[i]; ++i) {
  24.         kputc(SystemTable, str[i]);
  25.     }
  26. }

  27. void number(CHAR16 *buff, UINTN value, UINTN base) {
  28.     static const CHAR16 table[16] = L"0123456789abcdef";
  29.     if(base > 16) base = 16;
  30.     CHAR16 *p = buff;
  31.     do {
  32.         *p++ = table[value % base];
  33.         value /= base;
  34.     } while(value);
  35.     *p = L'\0';
  36.     UINTN count = p - buff;
  37.     for(UINTN i = 0; i < count / 2; ++i) {
  38.         swap(&buff[i], &buff[count - i - 1]);
  39.     }
  40. }

  41. void print_dec(EFI_SYSTEM_TABLE *SystemTable, UINTN value) {
  42.     CHAR16 buff[128]; number(buff, value, 10); kputs(SystemTable, buff); kputc(SystemTable, L'\n');
  43. }

  44. void print_hex(EFI_SYSTEM_TABLE *SystemTable, UINTN value) {
  45.     CHAR16 buff[128]; number(buff, value, 16); kputs(SystemTable, buff); kputc(SystemTable, L'\n');
  46. }

  47. void info_graphics(EFI_SYSTEM_TABLE *SystemTable, EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput) {
  48.     kputs(SystemTable, L"FrameBufferBase: "); print_hex(SystemTable, GraphicsOutput->Mode->FrameBufferBase);
  49.     kputs(SystemTable, L"FrameBufferSize: "); print_dec(SystemTable, GraphicsOutput->Mode->FrameBufferSize);
  50.     kputs(SystemTable, L"FrameBufferSize: "); print_hex(SystemTable, GraphicsOutput->Mode->FrameBufferSize);
  51.     kputs(SystemTable, L"MaxMode: "); print_dec(SystemTable, GraphicsOutput->Mode->MaxMode);
  52.     kputs(SystemTable, L"Mode: "); print_dec(SystemTable, GraphicsOutput->Mode->Mode);
  53.     kputs(SystemTable, L"SizeOfInfo: "); print_dec(SystemTable, GraphicsOutput->Mode->SizeOfInfo);
  54.     kputs(SystemTable, L"HorizontalResolution: "); print_dec(SystemTable, GraphicsOutput->Mode->Info->HorizontalResolution);
  55.     kputs(SystemTable, L"PixelsPerScanLine: "); print_dec(SystemTable, GraphicsOutput->Mode->Info->PixelsPerScanLine);
  56.     kputs(SystemTable, L"PixelFormat: "); print_dec(SystemTable, GraphicsOutput->Mode->Info->PixelFormat);
  57.     kputs(SystemTable, L"Version: "); print_dec(SystemTable, GraphicsOutput->Mode->Info->Version);
  58.     kputs(SystemTable, L"VerticalResolution: "); print_dec(SystemTable, GraphicsOutput->Mode->Info->VerticalResolution);
  59.     kputs(SystemTable, L"BlueMask: "); print_dec(SystemTable, GraphicsOutput->Mode->Info->PixelInformation.BlueMask);
  60.     kputs(SystemTable, L"GreenMask: "); print_dec(SystemTable, GraphicsOutput->Mode->Info->PixelInformation.GreenMask);
  61.     kputs(SystemTable, L"RedMask: "); print_dec(SystemTable, GraphicsOutput->Mode->Info->PixelInformation.RedMask);
  62.     kputs(SystemTable, L"ReservedMask: "); print_dec(SystemTable, GraphicsOutput->Mode->Info->PixelInformation.ReservedMask);
  63.     kgetc(SystemTable);
  64.     for(UINTN i = 0; i < GraphicsOutput->Mode->MaxMode; ++i) {
  65.         kputs(SystemTable, L"i: "); print_dec(SystemTable, i);
  66.         EFI_GRAPHICS_OUTPUT_MODE_INFORMATION *GraphicsOutputMode;
  67.         UINTN SizeOfInfo;
  68.         GraphicsOutput->QueryMode(GraphicsOutput, i, &SizeOfInfo, &GraphicsOutputMode);
  69.         kputs(SystemTable, L"SizeOfInfo: "); print_dec(SystemTable, SizeOfInfo);
  70.         kputs(SystemTable, L"HorizontalResolution: "); print_dec(SystemTable, GraphicsOutputMode->HorizontalResolution);
  71.         kputs(SystemTable, L"PixelsPerScanLine: "); print_dec(SystemTable, GraphicsOutputMode->PixelsPerScanLine);
  72.         kputs(SystemTable, L"PixelFormat: "); print_dec(SystemTable, GraphicsOutputMode->PixelFormat);
  73.         kputs(SystemTable, L"Version: "); print_dec(SystemTable, GraphicsOutputMode->Version);
  74.         kputs(SystemTable, L"VerticalResolution: "); print_dec(SystemTable, GraphicsOutputMode->VerticalResolution);
  75.         kputs(SystemTable, L"BlueMask: "); print_dec(SystemTable, GraphicsOutputMode->PixelInformation.BlueMask);
  76.         kputs(SystemTable, L"GreenMask: "); print_dec(SystemTable, GraphicsOutputMode->PixelInformation.GreenMask);
  77.         kputs(SystemTable, L"RedMask: "); print_dec(SystemTable, GraphicsOutputMode->PixelInformation.RedMask);
  78.         kputs(SystemTable, L"ReservedMask: "); print_dec(SystemTable, GraphicsOutputMode->PixelInformation.ReservedMask);
  79.         kgetc(SystemTable);
  80.     }
  81. }

  82. void fill_screen(EFI_SYSTEM_TABLE *SystemTable, EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput) {
  83.     //GraphicsOutput->SetMode(GraphicsOutput, 22);    // 1920x1080
  84.     GraphicsOutput->SetMode(GraphicsOutput, 18);    // 1440x900
  85.     EFI_GRAPHICS_OUTPUT_BLT_PIXEL *BltBuffer = (EFI_GRAPHICS_OUTPUT_BLT_PIXEL *)GraphicsOutput->Mode->FrameBufferBase;
  86.     UINTN height = GraphicsOutput->Mode->Info->VerticalResolution;
  87.     UINTN width = GraphicsOutput->Mode->Info->HorizontalResolution;
  88.     EFI_GRAPHICS_OUTPUT_BLT_PIXEL color = {255, 255, 255};
  89.     for(UINTN y = 0; y < height; ++y) {
  90.         for(UINTN x = 0; x < width; ++x) {
  91.             BltBuffer[y * width + x] = color;
  92.         }
  93.     }
  94. }

  95. void graphics(EFI_SYSTEM_TABLE *SystemTable) {
  96.     EFI_GUID guid = EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID;
  97.     EFI_GRAPHICS_OUTPUT_PROTOCOL *GraphicsOutput;
  98.     SystemTable->BootServices->LocateProtocol(&guid, NULL, (VOID **)&GraphicsOutput);
  99.     info_graphics(SystemTable, GraphicsOutput);
  100.     kgetc(SystemTable);
  101.     fill_screen(SystemTable, GraphicsOutput);
  102.     kgetc(SystemTable);
  103. }

  104. void info_mouse(EFI_SYSTEM_TABLE *SystemTable, EFI_SIMPLE_POINTER_PROTOCOL *SimplePointer) {
  105.     kputs(SystemTable, L"ResolutionX: "); print_hex(SystemTable, SimplePointer->Mode->ResolutionX);
  106.     kputs(SystemTable, L"ResolutionY: "); print_hex(SystemTable, SimplePointer->Mode->ResolutionY);
  107.     kputs(SystemTable, L"ResolutionZ: "); print_hex(SystemTable, SimplePointer->Mode->ResolutionZ);
  108.     kputs(SystemTable, L"LeftButton: "); print_hex(SystemTable, SimplePointer->Mode->LeftButton);
  109.     kputs(SystemTable, L"RightButton: "); print_hex(SystemTable, SimplePointer->Mode->RightButton);
  110. }

  111. void mouse_input(EFI_SYSTEM_TABLE *SystemTable) {
  112.     EFI_GUID guid = EFI_SIMPLE_POINTER_PROTOCOL_GUID;
  113.     EFI_SIMPLE_POINTER_PROTOCOL *SimplePointer;
  114.     SystemTable->BootServices->LocateProtocol(&guid, NULL, (VOID **)&SimplePointer);
  115.     info_mouse(SystemTable, SimplePointer);

  116.     UINTN count = 100;
  117.     EFI_SIMPLE_POINTER_STATE State;
  118.     while(count--) {
  119.         kputs(SystemTable, L"count: "); print_hex(SystemTable, count);
  120.         SystemTable->BootServices->WaitForEvent(1, &SimplePointer->WaitForInput, NULL);
  121.         SimplePointer->GetState(SimplePointer, &State);
  122.         kputs(SystemTable, L"RelativeMovementX: "); print_hex(SystemTable, State.RelativeMovementX);
  123.         kputs(SystemTable, L"RelativeMovementY: "); print_hex(SystemTable, State.RelativeMovementY);
  124.         kputs(SystemTable, L"RelativeMovementZ: "); print_hex(SystemTable, State.RelativeMovementZ);
  125.         kputs(SystemTable, L"LeftButton: "); print_hex(SystemTable, State.LeftButton);
  126.         kputs(SystemTable, L"RightButton: "); print_hex(SystemTable, State.RightButton);
  127.     }
  128. }

  129. void info_st(EFI_SYSTEM_TABLE *SystemTable) {
  130.     kputs(SystemTable, L"FirmwareVendor: "); kputs(SystemTable, SystemTable->FirmwareVendor); kputc(SystemTable, L'\n');
  131.     print_variable(SystemTable, hex, SystemTable->ConOut->Mode->Mode);
  132.     print_variable(SystemTable, hex, SystemTable->ConOut->Mode->MaxMode);
  133.     print_variable(SystemTable, hex, SystemTable->ConOut->Mode->Attribute);
  134.     print_variable(SystemTable, hex, SystemTable->ConOut->Mode->CursorColumn);
  135.     print_variable(SystemTable, hex, SystemTable->ConOut->Mode->CursorRow);
  136.     print_variable(SystemTable, hex, SystemTable->ConOut->Mode->CursorVisible);
  137.     print_variable(SystemTable, hex, SystemTable->NumberOfTableEntries);
  138.     for(UINTN i = 0; i < SystemTable->ConOut->Mode->MaxMode; ++i) {
  139.         print_variable(SystemTable, hex, i);
  140.         UINTN Columns, Rows;
  141.         SystemTable->ConOut->QueryMode(SystemTable->ConOut, i, &Columns, &Rows);
  142.         print_variable(SystemTable, hex, Columns);
  143.         print_variable(SystemTable, hex, Rows);
  144.     }
  145.     for(UINTN b = 0x00; b <= 0x70; b += 0x10) {
  146.         for(UINTN f = 0x00; f <= 0x0f; ++f) {
  147.             SystemTable->ConOut->SetAttribute(SystemTable->ConOut, b | f);
  148.             kputs(SystemTable, L"hello world!\n");
  149.             kgetc(SystemTable);
  150.         }
  151.     }
  152. }

  153. EFI_STATUS EFIAPI efi_main(EFI_HANDLE *ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
  154.     graphics(SystemTable);
  155.     mouse_input(SystemTable);
  156.     info_st(SystemTable);
  157.     kgetc(SystemTable);
  158.     return EFI_SUCCESS;
  159. }
  160. $ x86_64-w64-mingw32-gcc -Wall -e efi_main -nostdlib -fno-builtin -Wl,--subsystem,10 -I /usr/include/efi -I /usr/include/efi/x86_64 -I /usr/include/efi/protocol -o main.efi main.c
  161. $
复制代码

小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-28 16:25:14 | 显示全部楼层
下面代码输出smbios中的相当一少部分信息
因为smbios里面的信息是真的非常非常的多
我使用的这个库(gnu-efi)只写了很少一部分struct
我当然可以选择自己照着标准文档写,也可以选择复制 dmidecode 中的struct定义,我想 dmidecode 里面应该有这些定义吧,不然他怎么解析smbios
不过吗,我并不是要写一个类似 dmidecode 的软件,就不这么整了,有什么就写什么吧(其实就是因为嫌麻烦,^_^)

  1. #include <efi.h>
  2. #include <libsmbios.h>

  3. #define print_variable(SystemTable, base, value) \
  4.     kputs(SystemTable, L"" #value ": "); print_##base(SystemTable, value); kputc(SystemTable, L'\n')

  5. void swap(CHAR16 *a, CHAR16 *b) {
  6.     CHAR16 temp = *a;
  7.     *a = *b;
  8.     *b = temp;
  9. }

  10. CHAR16 kgetc(EFI_SYSTEM_TABLE *SystemTable) {
  11.     EFI_INPUT_KEY key;
  12.     SystemTable->BootServices->WaitForEvent(1, &SystemTable->ConIn->WaitForKey, NULL);
  13.     SystemTable->ConIn->ReadKeyStroke(SystemTable->ConIn, &key);
  14.     if(key.UnicodeChar == '\r') key.UnicodeChar = '\n';
  15.     return key.UnicodeChar;
  16. }

  17. void kputc(EFI_SYSTEM_TABLE *SystemTable, CHAR16 c) {
  18.     if(c == L'\n') kputc(SystemTable, L'\r');
  19.     CHAR16 str[2] = {c, '\0'};
  20.     SystemTable->ConOut->OutputString(SystemTable->ConOut, str);
  21. }

  22. void kputs(EFI_SYSTEM_TABLE *SystemTable, const CHAR16 *str) {
  23.     for(UINTN i = 0; str[i]; ++i) {
  24.         kputc(SystemTable, str[i]);
  25.     }
  26. }

  27. void number(CHAR16 *buff, UINTN value, UINTN base) {
  28.     static const CHAR16 table[16] = L"0123456789abcdef";
  29.     if(base > 16) base = 16;
  30.     CHAR16 *p = buff;
  31.     do {
  32.         *p++ = table[value % base];
  33.         value /= base;
  34.     } while(value);
  35.     *p = L'\0';
  36.     UINTN count = p - buff;
  37.     for(UINTN i = 0; i < count / 2; ++i) {
  38.         swap(&buff[i], &buff[count - i - 1]);
  39.     }
  40. }

  41. void print_dec(EFI_SYSTEM_TABLE *SystemTable, UINTN value) {
  42.     CHAR16 buff[128]; number(buff, value, 10); kputs(SystemTable, buff);
  43. }

  44. void print_hex(EFI_SYSTEM_TABLE *SystemTable, UINTN value) {
  45.     CHAR16 buff[128]; number(buff, value, 16); kputs(SystemTable, buff);
  46. }

  47. /*
  48. typedef struct {
  49.     UINT32  Data1;
  50.     UINT16  Data2;
  51.     UINT16  Data3;
  52.     UINT8   Data4[8];
  53. } EFI_GUID;
  54. */
  55. BOOLEAN compare_guid(const EFI_GUID *a, const EFI_GUID *b) {
  56.     if(a->Data1 != b->Data1) return FALSE;
  57.     if(a->Data2 != b->Data2) return FALSE;
  58.     if(a->Data3 != b->Data3) return FALSE;
  59.     for(UINTN i = 0; i < 8; ++i) {
  60.         if(a->Data4[i] != b->Data4[i]) return FALSE;
  61.     }
  62.     return TRUE;
  63. }

  64. void print_guid(EFI_SYSTEM_TABLE *SystemTable, const EFI_GUID *guid) {
  65.     const UINT8 *base = (const UINT8 *)guid;
  66.     kputc(SystemTable, L'{');
  67.     BOOLEAN flag = FALSE;
  68.     for(UINTN i = 0; i < 16; ++i) {
  69.         if(flag) kputs(SystemTable, L", ");
  70.         flag = TRUE;
  71.         print_hex(SystemTable, base[i]);
  72.     }
  73.     kputc(SystemTable, L'}');
  74. }

  75. UINT8 *print_smbios_string(EFI_SYSTEM_TABLE *SystemTable, UINT8 *str) {
  76.     if(!*str) ++str;
  77.     for(UINTN i = 1; ; ++i) {
  78.         if(!*str) break;
  79.         print_dec(SystemTable, i);
  80.         kputs(SystemTable, L": ");
  81.         while(*str) kputc(SystemTable, *str++);
  82.         kputc(SystemTable, L'\n');
  83.         ++str;
  84.     }
  85.     return str + 1;
  86. }

  87. void print_smbios_header(EFI_SYSTEM_TABLE *SystemTable, const SMBIOS_HEADER *hdr) {
  88.     print_variable(SystemTable, hex, hdr->Type);
  89.     print_variable(SystemTable, hex, hdr->Length);
  90.     print_variable(SystemTable, hex, *(UINT16 *)&hdr->Handle);
  91. }

  92. void *bios_information(EFI_SYSTEM_TABLE *SystemTable, void *base) {
  93.     SMBIOS_TYPE0 *st0 = base;
  94.     kputc(SystemTable, L'\n');
  95.     print_smbios_header(SystemTable, &st0->Hdr);
  96.     print_variable(SystemTable, hex, st0->Vendor);
  97.     print_variable(SystemTable, hex, st0->BiosVersion);
  98.     print_variable(SystemTable, hex, *(UINT16 *)&st0->BiosSegment);
  99.     print_variable(SystemTable, hex, st0->BiosReleaseDate);
  100.     print_variable(SystemTable, hex, st0->BiosSize);
  101.     print_variable(SystemTable, hex, st0->BiosCharacteristics[0]);
  102.     print_variable(SystemTable, hex, st0->BiosCharacteristics[1]);
  103.     print_variable(SystemTable, hex, st0->BiosCharacteristics[2]);
  104.     print_variable(SystemTable, hex, st0->BiosCharacteristics[3]);
  105.     print_variable(SystemTable, hex, st0->BiosCharacteristics[4]);
  106.     print_variable(SystemTable, hex, st0->BiosCharacteristics[5]);
  107.     print_variable(SystemTable, hex, st0->BiosCharacteristics[6]);
  108.     print_variable(SystemTable, hex, st0->BiosCharacteristics[7]);
  109.     return print_smbios_string(SystemTable, base + st0->Hdr.Length);
  110. }

  111. void *system_information(EFI_SYSTEM_TABLE *SystemTable, void *base) {
  112.     SMBIOS_TYPE1 *st1 = base;
  113.     kputc(SystemTable, L'\n');
  114.     print_smbios_header(SystemTable, &st1->Hdr);
  115.     print_variable(SystemTable, hex, st1->Manufacturer);
  116.     print_variable(SystemTable, hex, st1->ProductName);
  117.     print_variable(SystemTable, hex, st1->Version);
  118.     print_variable(SystemTable, hex, st1->SerialNumber);
  119.     print_guid(SystemTable, &st1->Uuid); kputc(SystemTable, L'\n');
  120.     print_variable(SystemTable, hex, st1->WakeUpType);
  121.     return print_smbios_string(SystemTable, base + st1->Hdr.Length);
  122. }

  123. void *baseboard_information(EFI_SYSTEM_TABLE *SystemTable, void *base) {
  124.     SMBIOS_TYPE2 *st2 = base;
  125.     kputc(SystemTable, L'\n');
  126.     print_smbios_header(SystemTable, &st2->Hdr);
  127.     print_variable(SystemTable, hex, st2->Manufacturer);
  128.     print_variable(SystemTable, hex, st2->ProductName);
  129.     print_variable(SystemTable, hex, st2->Version);
  130.     print_variable(SystemTable, hex, st2->SerialNumber);
  131.     return print_smbios_string(SystemTable, base + st2->Hdr.Length);
  132. }

  133. void *system_enclosure(EFI_SYSTEM_TABLE *SystemTable, void *base) {
  134.     SMBIOS_TYPE3 *st3 = base;
  135.     kputc(SystemTable, L'\n');
  136.     print_smbios_header(SystemTable, &st3->Hdr);
  137.     print_variable(SystemTable, hex, st3->Manufacturer);
  138.     print_variable(SystemTable, hex, st3->Type);
  139.     print_variable(SystemTable, hex, st3->Version);
  140.     print_variable(SystemTable, hex, st3->SerialNumber);
  141.     print_variable(SystemTable, hex, st3->AssetTag);
  142.     print_variable(SystemTable, hex, st3->BootupState);
  143.     print_variable(SystemTable, hex, st3->PowerSupplyState);
  144.     print_variable(SystemTable, hex, st3->ThermalState);
  145.     print_variable(SystemTable, hex, st3->SecurityStatus);
  146.     print_variable(SystemTable, hex, *(UINT32 *)&st3->OemDefined);
  147.     return print_smbios_string(SystemTable, base + st3->Hdr.Length);
  148. }

  149. void *processor_information(EFI_SYSTEM_TABLE *SystemTable, void *base) {
  150.     SMBIOS_TYPE4 *st4 = base;
  151.     kputc(SystemTable, L'\n');
  152.     print_smbios_header(SystemTable, &st4->Hdr);
  153.     print_variable(SystemTable, hex, st4->Socket);
  154.     print_variable(SystemTable, hex, st4->ProcessorType);
  155.     print_variable(SystemTable, hex, st4->ProcessorFamily);
  156.     print_variable(SystemTable, hex, st4->ProcessorId[0]);
  157.     print_variable(SystemTable, hex, st4->ProcessorId[1]);
  158.     print_variable(SystemTable, hex, st4->ProcessorId[2]);
  159.     print_variable(SystemTable, hex, st4->ProcessorId[3]);
  160.     print_variable(SystemTable, hex, st4->ProcessorId[4]);
  161.     print_variable(SystemTable, hex, st4->ProcessorId[5]);
  162.     print_variable(SystemTable, hex, st4->ProcessorId[6]);
  163.     print_variable(SystemTable, hex, st4->ProcessorId[7]);
  164.     print_variable(SystemTable, hex, st4->ProcessorVersion);
  165.     print_variable(SystemTable, hex, st4->Voltage);
  166.     print_variable(SystemTable, hex, *(UINT16 *)&st4->ExternalClock);
  167.     print_variable(SystemTable, hex, *(UINT16 *)&st4->MaxSpeed);
  168.     print_variable(SystemTable, hex, *(UINT16 *)&st4->CurrentSpeed);
  169.     print_variable(SystemTable, hex, st4->Status);
  170.     print_variable(SystemTable, hex, st4->ProcessorUpgrade);
  171.     print_variable(SystemTable, hex, *(UINT16 *)&st4->L1CacheHandle);
  172.     print_variable(SystemTable, hex, *(UINT16 *)&st4->L2CacheHandle);
  173.     print_variable(SystemTable, hex, *(UINT16 *)&st4->L3CacheHandle);
  174.     return print_smbios_string(SystemTable, base + st4->Hdr.Length);
  175. }

  176. void *unknown_type(EFI_SYSTEM_TABLE *SystemTable, void *base) {
  177.     SMBIOS_HEADER *hdr = base;
  178.     kputc(SystemTable, L'\n');
  179.     print_smbios_header(SystemTable, hdr);
  180.     return print_smbios_string(SystemTable, base + hdr->Length);
  181. }

  182. void info_eps(EFI_SYSTEM_TABLE *SystemTable) {
  183.     EFI_GUID smbios_guid = SMBIOS_TABLE_GUID;
  184.     EFI_GUID smbios3_guid = SMBIOS3_TABLE_GUID;
  185.     EFI_GUID mps_guid = MPS_TABLE_GUID;
  186.     EFI_GUID acpi_guid = ACPI_TABLE_GUID;
  187.     EFI_GUID acpi20_guid = ACPI_20_TABLE_GUID;
  188.     EFI_GUID sal_system_guid = SAL_SYSTEM_TABLE_GUID;
  189.     EFI_GUID dtb_guid = EFI_DTB_TABLE_GUID;
  190.     SMBIOS_STRUCTURE_TABLE *smbios_header = NULL;
  191.     for(UINTN i = 0; i < SystemTable->NumberOfTableEntries; ++i) {
  192.         if(compare_guid(&SystemTable->ConfigurationTable[i].VendorGuid, &smbios_guid)) {
  193.             kputs(SystemTable, L"SMBIOS_TABLE_GUID: ");
  194.             smbios_header = SystemTable->ConfigurationTable[i].VendorTable;
  195.         }
  196.         if(compare_guid(&SystemTable->ConfigurationTable[i].VendorGuid, &smbios3_guid)) kputs(SystemTable, L"SMBIOS3_TABLE_GUID: ");
  197.         if(compare_guid(&SystemTable->ConfigurationTable[i].VendorGuid, &mps_guid)) kputs(SystemTable, L"MPS_TABLE_GUID: ");
  198.         if(compare_guid(&SystemTable->ConfigurationTable[i].VendorGuid, &acpi_guid)) kputs(SystemTable, L"ACPI_TABLE_GUID: ");
  199.         if(compare_guid(&SystemTable->ConfigurationTable[i].VendorGuid, &acpi20_guid)) kputs(SystemTable, L"ACPI_20_TABLE_GUID: ");
  200.         if(compare_guid(&SystemTable->ConfigurationTable[i].VendorGuid, &sal_system_guid)) kputs(SystemTable, L"SAL_SYSTEM_TABLE_GUID: ");
  201.         if(compare_guid(&SystemTable->ConfigurationTable[i].VendorGuid, &dtb_guid)) kputs(SystemTable, L"EFI_DTB_TABLE_GUID: ");
  202.         print_guid(SystemTable, &SystemTable->ConfigurationTable[i].VendorGuid); kputc(SystemTable, L'\n');
  203.     }
  204.     if(!smbios_header) return;
  205.     kputs(SystemTable, L"AnchorString: "");
  206.     kputc(SystemTable, smbios_header->AnchorString[0]);
  207.     kputc(SystemTable, smbios_header->AnchorString[1]);
  208.     kputc(SystemTable, smbios_header->AnchorString[2]);
  209.     kputc(SystemTable, smbios_header->AnchorString[3]);
  210.     kputs(SystemTable, L""\n");
  211.     print_variable(SystemTable, hex, smbios_header->EntryPointStructureChecksum);
  212.     print_variable(SystemTable, hex, smbios_header->EntryPointLength);
  213.     print_variable(SystemTable, hex, smbios_header->MajorVersion);
  214.     print_variable(SystemTable, hex, smbios_header->MinorVersion);
  215.     print_variable(SystemTable, hex, smbios_header->MaxStructureSize);
  216.     print_variable(SystemTable, hex, smbios_header->EntryPointRevision);
  217.     print_variable(SystemTable, hex, smbios_header->IntermediateChecksum);
  218.     print_variable(SystemTable, hex, smbios_header->TableLength);
  219.     print_variable(SystemTable, hex, smbios_header->TableAddress);
  220.     print_variable(SystemTable, hex, smbios_header->NumberOfSmbiosStructures);
  221.     print_variable(SystemTable, hex, smbios_header->SmbiosBcdRevision);
  222.     kputs(SystemTable, L"IntermediateAnchorString: "");
  223.     kputc(SystemTable, smbios_header->IntermediateAnchorString[0]);
  224.     kputc(SystemTable, smbios_header->IntermediateAnchorString[1]);
  225.     kputc(SystemTable, smbios_header->IntermediateAnchorString[2]);
  226.     kputc(SystemTable, smbios_header->IntermediateAnchorString[3]);
  227.     kputc(SystemTable, smbios_header->IntermediateAnchorString[4]);
  228.     kputs(SystemTable, L""\n");
  229.     print_variable(SystemTable, hex, smbios_header->FormattedArea[0]);
  230.     print_variable(SystemTable, hex, smbios_header->FormattedArea[1]);
  231.     print_variable(SystemTable, hex, smbios_header->FormattedArea[2]);
  232.     print_variable(SystemTable, hex, smbios_header->FormattedArea[3]);
  233.     print_variable(SystemTable, hex, smbios_header->FormattedArea[4]);
  234.     UINT8 *next = (UINT8 *)(UINT64)smbios_header->TableAddress;
  235.     for(UINTN i = 0; i < smbios_header->NumberOfSmbiosStructures; ++i) {
  236.         switch(*next) {
  237.             case 0: next = bios_information(SystemTable, next); break;
  238.             case 1: next = system_information(SystemTable, next); break;
  239.             case 2: next = baseboard_information(SystemTable, next); break;
  240.             case 3: next = system_enclosure(SystemTable, next); break;
  241.             case 4: next = processor_information(SystemTable, next); break;
  242.             default: next = unknown_type(SystemTable, next);
  243.         }
  244.         kgetc(SystemTable);
  245.     }
  246. }

  247. EFI_STATUS EFIAPI efi_main(EFI_HANDLE *ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
  248.     info_eps(SystemTable);
  249.     kgetc(SystemTable);
  250.     return EFI_SUCCESS;
  251. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-10-28 16:58:32 | 显示全部楼层
2wsx 发表于 2022-10-13 16:23
大佬 你了解SMBIOS吗?我目前是希望写代码读取SMBIOS的任意的Type,并将其Handle打印出来,读取任意一个 ...

接下来再帮你研究研究pci配置空间
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-23 08:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表