Phoenix
Object-oriented orthogonally persistent operating system
efi.h
00001 /*
00002  * /phoenix/kernel/sys/efi.h
00003  *
00004  * This file is a part of Phoenix operating system.
00005  * Copyright (c) 2011-2012, Artyom Lebedev <artyom.lebedev@gmail.com>
00006  * All rights reserved.
00007  * See COPYING file for copyright details.
00008  */
00009 
00014 #ifndef EFI_H_
00015 #define EFI_H_
00016 
00020 namespace efi
00021 {
00022 
00024 typedef u8 Boolean;
00026 typedef intptr_t Intn;
00028 typedef uintptr_t Uintn;
00030 typedef uintptr_t EfiStatus;
00032 typedef u16 Char16;
00034 typedef void *Handle;
00036 typedef u64 PhysAddr;
00038 typedef u64 VirtAddr;
00039 
00040 constexpr EfiStatus EfiErr(const int num) {
00041     return 0x8000000000000000 | num;
00042 }
00043 
00044 enum EfiStatusCode {
00045     EFI_SUCCESS =           0,
00046     EFI_LOAD_ERROR =        EfiErr(1),
00047     EFI_INVALID_PARAMETER = EfiErr(2),
00048     EFI_UNSUPPORTED =       EfiErr(3),
00049     EFI_BAD_BUFFER_SIZE =   EfiErr(4),
00050     EFI_BUFFER_TOO_SMALL =  EfiErr(5),
00051     EFI_NOT_READY =         EfiErr(6),
00052     EFI_DEVICE_ERROR =      EfiErr(7),
00053     EFI_WRITE_PROTECTED =   EfiErr(8),
00054     EFI_OUT_OF_RESOURCES =  EfiErr(9),
00055     EFI_VOLUME_CORRUPTED =  EfiErr(10),
00056     EFI_VOLUME_FULL =       EfiErr(11),
00057     EFI_NO_MEDIA =          EfiErr(12),
00058     EFI_MEDIA_CHANGED =     EfiErr(13),
00059     EFI_NOT_FOUND =         EfiErr(14),
00060     EFI_ACCESS_DENIED =     EfiErr(15),
00061     EFI_NO_RESPONSE =       EfiErr(16),
00062     EFI_NO_MAPPING =        EfiErr(17),
00063     EFI_TIMEOUT =           EfiErr(18),
00064     EFI_NOT_STARTED =       EfiErr(19),
00065     EFI_ALREADY_STARTED =   EfiErr(20),
00066     EFI_ABORTED =           EfiErr(21),
00067     EFI_ICMP_ERROR =        EfiErr(22),
00068     EFI_TFTP_ERROR =        EfiErr(23),
00069     EFI_PROTOCOL_ERROR =    EfiErr(24),
00070 };
00071 
00073 struct Guid {
00074     u32 data1;
00075     u16 data2;
00076     u16 data3;
00077     u8 data4[8];
00078 
00079     //XXX initializer_list constructor
00080 };
00081 
00082 } /* namespace efi */
00083 
00084 #include <md_efi.h>
00085 
00086 namespace efi {
00087 
00091 class MemoryMap {
00092 public:
00094     enum MemType {
00095         EfiReservedMemoryType,
00096         EfiLoaderCode,
00097         EfiLoaderData,
00098         EfiBootServicesCode,
00099         EfiBootServicesData,
00100         EfiRuntimeServicesCode,
00101         EfiRuntimeServicesData,
00102         EfiConventionalMemory,
00103         EfiUnusableMemory,
00104         EfiACPIReclaimMemory,
00105         EfiACPIMemoryNVS,
00106         EfiMemoryMappedIO,
00107         EfiMemoryMappedIOPortSpace,
00108         EfiPalCode,
00109         EfiMaxMemoryType
00110     };
00111 
00113     enum MemAttr {
00117         EFI_MEMORY_UC =         0x1,
00121         EFI_MEMORY_WC =         0x2,
00126         EFI_MEMORY_WT =         0x4,
00132         EFI_MEMORY_WB =         0x8,
00137         EFI_MEMORY_UCE =        0x10,
00141         EFI_MEMORY_WP =         0x1000,
00145         EFI_MEMORY_RP =         0x2000,
00150         EFI_MEMORY_XP =         0x4000,
00155         EFI_MEMORY_RUNTIME =    0x8000000000000000
00156     };
00157 
00159     struct MemDesc {
00160         u32 type;
00161         u32 pad;
00162         PhysAddr paStart;
00163         VirtAddr vaStart;
00164         u64 numPages;
00165         u64 attr;
00166 
00168         bool IsAvailable() {
00169             return type == EfiLoaderCode || type == EfiLoaderData ||
00170                    type == EfiBootServicesCode || type == EfiBootServicesData ||
00171                    type == EfiConventionalMemory;
00172         }
00173 
00175         bool NeedsManagement() {
00176             return !(type == EfiReservedMemoryType || type == EfiUnusableMemory ||
00177                      type == EfiMemoryMappedIO || type == EfiMemoryMappedIOPortSpace) ||
00178                    (attr & EFI_MEMORY_RUNTIME);
00179         }
00180     };
00181 
00183     class MemDescIterator {
00184     public:
00185         inline MemDescIterator(void *start, size_t descSize, size_t idx) :
00186             _curPos(start), _descSize(descSize) {
00187 
00188             _curPos += descSize * idx;
00189         }
00190         inline bool operator !=(MemDescIterator &desc) { return _curPos != desc._curPos; }
00191         inline void operator ++() { _curPos += _descSize; }
00192         inline MemDesc &operator *() { return *static_cast<MemDesc *>(_curPos); }
00193     private:
00194         vm::Vaddr _curPos;
00195         size_t _descSize;
00196     };
00197 
00198     MemoryMap(void *memMap, size_t numDesc, size_t descSize, u32 descVersion);
00199 
00200     const char *GetTypeName(MemType type);
00201 
00205     RetCode SetVirtualAddressMap();
00206 
00207     /* Iterator interface */
00208     inline size_t size() { return _numDesc; }
00209     inline MemDescIterator begin() { return MemDescIterator(_memMap, _descSize, 0); }
00210     inline MemDescIterator end() { return MemDescIterator(_memMap, _descSize, _numDesc); }
00211 private:
00212     size_t _numDesc, _descSize;
00213     u32 _descVersion;
00214     void *_memMap;
00215 };
00216 
00218 struct EfiTime {
00219     u16 year;
00220     u8 month;
00221     u8 day;
00222     u8 hour;
00223     u8 minute;
00224     u8 second;
00225     u8 pad1;
00226     u32 nanosecond;
00227     i16 timeZone;
00228     u8 daylight;
00229     u8 pad2;
00230 };
00231 
00233 struct EfiTimeCaps {
00234     u32 resolution;
00235     u32 accuracy;
00236     Boolean setsToZero;
00237 };
00238 
00243 class SystemTable {
00244 public:
00253     SystemTable(vm::Paddr ptr,
00254                 void *memMap,
00255                 size_t memMapNumDesc,
00256                 size_t memMapDescSize,
00257                 u32 memMapDescVersion);
00258 
00259     /* Run-time services. */
00260 
00264     EfiStatus GetTime(EfiTime *time, EfiTimeCaps *caps = 0) {
00265         return _runtimeServices->getTime(time, caps);
00266     }
00267 
00268     // XXX add all runtime services
00269 
00270     EfiStatus SetVirtualAddressMap(Uintn mapSize, Uintn descSize,
00271                                    u32 descVersion, MemoryMap::MemDesc *virtualMap) {
00272 
00273         return _runtimeServices->setVirtualAddressMap(mapSize, descSize,
00274                                                       descVersion, virtualMap);
00275     }
00276 
00277 private:
00279     struct TableHeader {
00280         u64 signature;
00281         u32 revision;
00282         u32 headerSize;
00283         u32 crc32;
00284         u32 reserved;
00285     };
00286 
00288     struct Table {
00289         TableHeader hdr;
00290         Char16 *fwVendor;
00291         u32 fwRevision;
00292         Handle consoleInHandle;
00293         void *conIn;
00294         Handle consoleOutHandle;
00295         void *conOut;
00296         Handle stdErrHandle;
00297         void *stdErr;
00298         void *runtimeServices;
00299         void *bootServices;
00300         Uintn numTableEntries;
00301         void *configTable;
00302     };
00303 
00305     struct ConfigTable {
00306         Guid vendorGuid;
00307         void *vendorTable;
00308     };
00309 
00311     struct RuntimeServicesTable {
00312         TableHeader hdr;
00313         EfiCall /* Time services. */
00314                 getTime,
00315                 setTime,
00316                 getWakeupTime,
00317                 setWakeupTime,
00318                 /* Virtual memory services. */
00319                 setVirtualAddressMap,
00320                 convertPointer,
00321                 /* Variable services. */
00322                 getVariable,
00323                 getNextVariableName,
00324                 setVariable,
00325                 /* Miscellaneous services. */
00326                 getNextHighMonotonicCount,
00327                 resetSystem,
00328                 /* UEFI 2.0 capsule services. */
00329                 updateCapsule,
00330                 queryCapsuleCapabilities,
00331                 /* UEFI 2.0 miscellaneous services. */
00332                 queryVariableInfo;
00333     };
00334 
00335     enum {
00336         EFI_SYSTEM_TABLE_SIGNATURE = 0x5453595320494249,
00337         EFI_RUNTIME_SERVICES_SIGNATURE = 0x56524553544e5552,
00338     };
00339 
00340     Table *_sysTable;
00341     ConfigTable *_configTable;
00342     RuntimeServicesTable *_runtimeServices;
00343 };
00344 
00345 extern SystemTable *sysTable;
00346 
00347 static inline MemoryMap::MemDescIterator
00348 begin(MemoryMap &memMap)
00349 {
00350     return memMap.begin();
00351 }
00352 
00353 static inline MemoryMap::MemDescIterator
00354 end(MemoryMap &memMap)
00355 {
00356     return memMap.end();
00357 }
00358 
00359 } /* namespace efi */
00360 
00361 #endif /* EFI_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines