Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Play with UEFI

Takuya ASADA
February 16, 2013

Play with UEFI

Takuya ASADA

February 16, 2013
Tweet

More Decks by Takuya ASADA

Other Decks in Technology

Transcript

  1. UEFIͱ͸Կ͔ʁ • 90೥୅ʹIntelͱHPʹΑΓ։ൃ͞ΕͨɺIA32/IA64ڞ௨ͷϒʔτγεςϜ • PCʹґવͱͯ͠࢒ΔϨΨγʔͳ࢓૊ΈͰ͋ΔBIOSΛஔ͖׵͑ • CPUඇґଘͳΞʔΩςΫνϟ • CPUඇґଘͳυϥΠό •

    128partitionɾover 2TBͷσΟεΫΛαϙʔτ͢ΔGPTΛ࠾༻ • ωοτϫʔΫରԠͳͲͷଟ࠼ͳػೳʹରԠ • ϞδϡϥʔܕͷσβΠϯ • ߴ଎ىಈʹ΋د༩
  2. MBRͱ2TBͷน • MBRͷύʔςΟʔγϣϯςʔϒϧͰ͸ɺ։࢝ηΫλ஋ͱηΫλ਺ ͕32bit(4G sectors) • 4G sectors * 512byte

    = 2TB! • ύʔςΟʔγϣϯςʔϒϧม͑Δ͔ηΫλαΠζม͑Δ͔͠ͳ͍ ˠޓ׵ੑແ͘ͳΔ • όουϊ΢ϋ΢Ͱճආग़དྷͳ͍͜ͱ΋ແ͍͚Ͳ… • ΋͏͠ΜͲ͍͠GPTΛલఏͱ͍ͯ͠ΔUEFIʹҠߦ͠Α͏Α
  3. GPT

  4. UEFI͔Βͷϒʔτ ݫີʹ͸Ͳ͏ͳ͍ͬͯΔ͔ ҎԼͷΑ͏ͳUEFI NVRAM variablesʹΑΓىಈॲཧ੍͕ޚ͞ΕΔ • Boot####ɿϩʔυ͢ΔUEFI applicationͷPATHɾ
 ຢ͸σΟεΫͷσόΠεPATH •

    BootOrderɿBoot####ͷࢼߦॱংʢ഑ྻͰࢦఆʣ • BootNextɿ࣍ճىಈ࣌ʹϩʔυ͢ΔBoot####ʢBootOrderΑΓ༏ ઌɺҰ౓ىಈ͢Δͱ࡟আʣ • Timeoutɿઃఆඵ਺͚ͩBoot MenuΛදࣔʢࣗಈىಈΛ஗Ԇʣ
  5. UEFIͷ಺෦ߏ଄ PEI CPU init Chipset init Board init DXE Load

    drivers %FWJDF %SJWFST Boot Manager 4FSWJDFT UEFI Apps UEFI Bootl oader UEFI APIs OS Load UEFI apps UEFI specification PI specification
  6. UEFIͷ಺෦ߏ଄ PEI CPU init Chipset init Board init DXE Load

    drivers %FWJDF %SJWFST Boot Manager 4FSWJDFT UEFI Apps UEFI Bootl oader UEFI APIs OS Load UEFI apps UEFI specification PI specification ࠓ೔͸͜͜Λݟ͍͖ͯ·͢
  7. UEFI APPLICATIONͷछྨ • ೋछྨͷUEFI Application • ௨ৗͷUEFI Applicationɿऴྃ࣌ʹExit()Λίʔϧ͠ɺ UEFI ShellͳͲʹ੍ޚΛ໭͢

    • OS Loaderɿऴྃ࣌ʹExitBootServices()Λίʔϧ͠ɺ UEFI͕ར༻͍ͯͨ͠ࢿݯΛ։์੍ͯ͠ޚΛOS΁Ҡ͢
  8. MAIN() IN UEFI APPLICATION EFI_STATUS HelloMain ( IN EFI_HANDLE ImageHandle,

    IN EFI_SYSTEM_TABLE *SystemTable) ! Ҿ਺͸̎ݸʢImageHandleɺSystemTableʣ ImageHandle͸ϩʔυ͞ΕͨUEFI ApplicationͷΠϝʔδϑΝΠϧʹର͢Δϋϯυϧ SystemTableΛܦ༝ͯ͠શͯͷUEFI API΁ΞΫηε
  9. UEFI SYSTEM TABLE UEFI Applicationʹରͯ͠ެ։͢ΔΠϯλϑΣʔε͕શͯ ٧Ίࠐ·Εͨߏ଄ମ 80 June 27, 2012

    Version 2.3.1, Errata C #define EFI_2_30_SYSTEM_TABLE_REVISION ((2<<16) | (30)) #define EFI_2_20_SYSTEM_TABLE_REVISION ((2<<16) | (20)) #define EFI_2_10_SYSTEM_TABLE_REVISION ((2<<16) | (10)) #define EFI_2_00_SYSTEM_TABLE_REVISION ((2<<16) | (00)) #define EFI_1_10_SYSTEM_TABLE_REVISION ((1<<16) | (10)) #define EFI_1_02_SYSTEM_TABLE_REVISION ((1<<16) | (02)) #define EFI_SYSTEM_TABLE_REVISION EFI_2_30_SYSTEM_TABLE_REVISION #define EFI_SPECIFICATION_VERSION EFI_SYSTEM_TABLE_REVISION #define EFI_SYSTEM_TABLE_REVISION EFI_2_31_SYSTEM_TABLE_REVISION typedef struct { EFI_TABLE_HEADER Hdr; CHAR16 *FirmwareVendor; UINT32 FirmwareRevision; EFI_HANDLE ConsoleInHandle; EFI_SIMPLE_TEXT_INPUT_PROTOCOL *ConIn; EFI_HANDLE ConsoleOutHandle; EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *ConOut; EFI System Table EFI_HANDLE StandardErrorHandle; EFI_SIMPLE_TEXT_OUTPUT_PROTOCOL *StdErr; EFI_RUNTIME_SERVICES *RuntimeServices; EFI_BOOT_SERVICES *BootServices; UINTN NumberOfTableEntries; EFI_CONFIGURATION_TABLE *ConfigurationTable; } EFI_SYSTEM_TABLE; Parameters Hdr The table header for the EFI System Table. This header contains the EFI_SYSTEM_TABLE_SIGNATURE and EFI_SYSTEM_TABLE_REVISION values along with the size of the EFI_SYSTEM_TABLE structure and a 32-bit CRC to verify that the contents of the EFI System Table are valid. FirmwareVendor A pointer to a null terminated string that identifies the vendor that produces the system firmware for the platform.
  10. CONOUT, CONIN, STDERR #include <Library/UefiApplicationEntryPoint.h> #include <Library/UefiLib.h> EFI_STATUS EFIAPI UefiMain

    (IN EFI_HANDLE ImageHandle, IN EFI_SYSTEM_TABLE *SystemTable) { SystemTable->ConOut->OutputString(SystemTable->ConOut, L"Hello World\r\n"); return EFI_SUCCESS; } •ίϯιʔϧ΁ͷೖग़ྗAPI •จࣈΤϯίʔσΟϯά͸UTF-16Λ࢖༻
  11. RUNTIME SERVICES • ExitBootServices()ޙ΋UEFI͕OSʹରͯ͠ఏڙ͢ΔαʔϏε • ࠷௿ݶͷػೳͷΈ • Time (GetTime, SetTime...)

    • Virtual Memory (SetirtualAddressMap...) • Variable Services (GetVariable...) • Miscellaneous Services(ResetSystem...)
  12. BOOT SERVICES • ExitBootServices()·ͰUEFI Applicationʹఏڙ͢ΔαʔϏε • Task Priority Services (RaiseTPL...)

    • Memory Services (AllocatePages...) • Event & Timer Services (CreateEvent, SetTimer...) • Protocol Handler Services (HandleProtocol...) • Image Services (LoadImage, StartImage...) • Miscellaneous Services (Stall, CopyMem...) • Open and Close Protocol Services (OpenProtocol...) • Library Services (LocateProtocol...) • 32bit CRC Services (CalculateCrc32...)
  13. ఆٛ͞Ε͍ͯΔPROTOCOL • EFI Loaded Image • Device Path Protocol •

    UEFI Driver Model • Console Support • Media Access • PCI Bus Support • SCSI Driver Models and Bus Support • iSCSI Boot • USB Support • Debugger Support • Compression Algorithm • ACPI Protocols • TCP/IP, IPSec, FTP • ARP & DHCP • UDP & MTFTP • etc...
  14. PROTOCOLར༻ྫ #define EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID \ { \ 0x964e5b22, 0x6459, 0x11d2, {0x8e,

    0x39, 0x0, 0xa0, 0xc9, 0x69, 0x72, 0x3b} \ } EFI_GUID gEfiSimpleFileSystemProtocolGuid = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID; EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *Vol; EFI_FILE_HANDLE Root; EFI_FILE_HANDLE CurDir; EFI_FILE_HANDLE FileHandle; CHAR16 *FileName = L”hoge.txt”; ! gBS->HandleProtocol ( DeviceHandle, &gEfiSimpleFileSystemProtocolGuid, &Vol ); ! Vol->OpenVolume (Vol, &Root); ! CurDir->Open ( CurDir, &FileHandle, FileName, EFI_FILE_MODE_READ, 0 ); •ProtocolͷHandleΛܦ༝ͯ͠APIίʔϧ
  15. UEFIͷSDK • EDK IIɿΦʔϓϯιʔεͰ։ൃ͞Ε͍ͯΔUEFI࣮૷ͰɺσϑΝΫτελϯμʔυ
 άά͙ͬͯ͢ग़ͯ͘Δͷ͸͜Εɻ
 ୠ͠ɺϏϧυํ๏ͷυΩϡϝϯτΛΑ͘ݟΔͱtrunkΛνΣοΫΞ΢τ͍ͯ͠Δɻ
 ͦͯ͠ɺtrunkͩͱ৭ʑͱ஍ཕ͕͋ͬͯසൟʹ౿Ή… • UDK2010ɿIntel͕࡞͍ͬͯΔEDK IIͷstableϦϦʔεɻ


    ஍ཕΛ౿Έͨ͘ͳ͔ͬͨΒͬͪ͜Λ࢖ͬͨ΄͏͕ྑ͍ͷ͕ͩɺEDK IIʹ͔͠ແ͍ػೳ΋ ৭ʑͱଘࡏɻ • gnu-efiɿEDK II͸UEFIϑΝʔϜ΢ΣΞͷιʔείʔυΛશؚͯΜͰ͍Δ͕ɺͪ͜Β͸UEFI ApplicationΛ࣮૷͢ΔͨΊ͚ͩͷLightweightͳ࣮૷ɻ
 γϯϓϧ؆୯͓खܰͰී௨ͷMakefile΋࢖͑Δ͠ɺLinux΍OSXϢʔβʹͱͬͯ͸ͬͪ͜ͷ ํ͕࢖͍΍ͦ͢͏ɻɹ…͕ͩɺ͜ΕͰ΋৭ʑͱ౿Μͩ…
  16. OVMF FOR QEMU/KVM • EDK II্ͰϏϧυͰ͖ΔQEMUɾKVM༻ͷUEFI࣮૷ • ίϚϯυҰൃͰϏϧυˍ࣮ߦ·Ͱ؆୯ʹग़དྷΔ • QEMUͰdebugconΛ༗ޮʹ͢ΔͱUEFI͔Βͷσόοάग़ྗ͕ಘΒΕΔ

    • γϦΞϧίϯιʔϧΛ༗ޮʹ͢ΔͱVGAଆͱಉ౳ͷग़ྗ͕ಘΒΕΔ • ͪΐͬͱઃఆ͢Ε͹gdb΋ܨ͕Δ͸ͣ • stable൛͡Όͳ͍ͷ͕ܹ͘͠ؾʹͳΔ͚ͲͦΜͳ΋Μଘࡏ͠ͳ͍ͷͰݟͳ ͔ͬͨ͜ͱʹ͢Δ͔͠…
  17. μϝϙΠϯτ̍ • ͜ͷϓϩάϥϜɺϝοηʔδ͕೔ຊޠͩ… if(access_token == NULL) { //ϦΫΤεττʔΫϯऔಘ if(errcode =

    Twitter_GetRequestToken(c, r), errcode < 0) { - printf("ϦΫΤεττʔΫϯͷऔಘͰΤϥʔ͕ൃੜ͍ͯ͠·͢\n"); - printf("Τϥʔίʔυ:%d\n", errcode); + printf("error occured during get request token\n"); + printf("error code:%d\n", errcode); exit(0); }
  18. μϝϙΠϯτ̏ • ඍົͳܕͷζϨ //࣌ؒΛऔಘͯ͠λΠϜελϯϓͱҰҙͳ஋Ληοτ gettimeofday(&tv, NULL); - sprintf(tstamp, "%ld", tv.tv_sec);

    + sprintf(tstamp, "%d", tv.tv_sec); - sprintf(nonce_tmp, "%ld", tv.tv_usec); + sprintf(nonce_tmp, "%d", tv.tv_usec); base64_encode(nonce_tmp, strlen(nonce_tmp), nonce, 128); URLEncode(nonce, nonce_urlenc);