types.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. /*++
  2. Copyright (c) 2014 Minoca Corp.
  3. This file is licensed under the terms of the GNU General Public License
  4. version 3. Alternative licensing terms are available. Contact
  5. info@minocacorp.com for details. See the LICENSE file at the root of this
  6. project for complete licensing information.
  7. Module Name:
  8. types.h
  9. Abstract:
  10. This header contains basic type definitions for UEFI.
  11. Author:
  12. Evan Green 7-Feb-2014
  13. --*/
  14. //
  15. // ------------------------------------------------------------------- Includes
  16. //
  17. #if defined(__i386)
  18. #include <minoca/uefi/x86/procdef.h>
  19. #elif defined(__arm__)
  20. #include <minoca/uefi/arm/procdef.h>
  21. #elif defined(__amd64)
  22. #include <minoca/uefi/x64/procdef.h>
  23. #else
  24. #error No known architecture was defined.
  25. #endif
  26. //
  27. // --------------------------------------------------------------------- Macros
  28. //
  29. //
  30. // Define vararg macros.
  31. //
  32. #define VA_START(_Marker, _Parameter) __builtin_va_start(_Marker, _Parameter)
  33. #define VA_ARG(_Marker, _Type) __builtin_va_arg(_Marker, _Type)
  34. #define VA_END(_Marker) __builtin_va_end(_Marker)
  35. #define VA_COPY(_Destination, _Source) __builtin_va_copy(_Destination, _Source)
  36. //
  37. // This macro returns the offset of the given field within the given structure.
  38. //
  39. #ifndef OFFSET_OF
  40. #define OFFSET_OF(_Type, _Field) ((UINTN) &(((_Type *)0)->_Field))
  41. #endif
  42. //
  43. // This macro rounds a value up to the next boundary using a given power of two
  44. // alignment.
  45. //
  46. #define ALIGN_VALUE(_Value, _Alignment) \
  47. ((_Value) + (((_Alignment) - (_Value)) & ((_Alignment) - 1)))
  48. //
  49. // This macro aligns a pointer by adding the minimum offset required for it to
  50. // be aligned on the given alignment boundary.
  51. //
  52. #define ALIGN_POINTER(_Pointer, _Alignment) \
  53. ((VOID *)(ALIGN_VALUE((UINTN)(_Pointer), (_Alignment))))
  54. //
  55. // This macro aligns the given variable up to the next natural boundary for the
  56. // current CPU (4 bytes for 32-bit CPUs and 8 bytes for 64-bit CPUs).
  57. //
  58. #define ALIGN_VARIABLE(_Value) ALIGN_VALUE((_Value), sizeof(UINTN))
  59. //
  60. // This macro returns the absolute value of the given integer.
  61. //
  62. #define ABS(_Value) (((_Value) < 0) ? (-(_Value)) : (_Value))
  63. //
  64. // This macro returns the maximum of the two given integers.
  65. //
  66. #define MAX(_FirstValue, _SecondValue) \
  67. (((_FirstValue) > (_SecondValue)) ? (_FirstValue) : (_SecondValue))
  68. //
  69. // This macro returns the minimum of the two given integers.
  70. //
  71. #define MIN(_FirstValue, _SecondValue) \
  72. (((_FirstValue) < (_SecondValue)) ? (_FirstValue) : (_SecondValue))
  73. //
  74. // This macro creates an error value.
  75. //
  76. #define ENCODE_ERROR(_StatusCode) ((RETURN_STATUS)(MAX_BIT | (_StatusCode)))
  77. #define EFIERR(_StatusCode) ENCODE_ERROR(_StatusCode)
  78. //
  79. // This macro creates a warning value.
  80. //
  81. #define ENCODE_WARNING(_StatusCode) ((RETURN_STATUS)(_StatusCode))
  82. //
  83. // This macro returns non-zero if the given status code has the high bit set.
  84. //
  85. #define RETURN_ERROR(_StatusCode) (((INTN)(RETURN_STATUS)(_StatusCode)) < 0)
  86. #define EFI_ERROR(_StatusCode) RETURN_ERROR(_StatusCode)
  87. //
  88. // This macro converts a size in bytes into a number of EFI_PAGES.
  89. //
  90. #define EFI_SIZE_TO_PAGES(_Bytes) \
  91. (((_Bytes) >> EFI_PAGE_SHIFT) + (((_Bytes) & EFI_PAGE_MASK) ? 1 : 0))
  92. //
  93. // This macro converts a EFI_PAGE count into a number of bytes.
  94. //
  95. #define EFI_PAGES_TO_SIZE(_Pages) ((_Pages) << EFI_PAGE_SHIFT)
  96. //
  97. // This macro determines whether or not a given machine type matches supported
  98. // machine types on this processor.
  99. //
  100. #if defined(EFI_X86)
  101. #define EFI_IMAGE_MACHINE_TYPE_SUPPORTED(_Machine) \
  102. ((_Machine) == EFI_IMAGE_MACHINE_IA32)
  103. #elif defined(EFI_ARM)
  104. #define EFI_IMAGE_MACHINE_TYPE_SUPPORTED(_Machine) \
  105. ((_Machine) == EFI_IMAGE_MACHINE_ARMTHUMB_MIXED)
  106. #elif defined(EFI_X64)
  107. #define EFI_IMAGE_MACHINE_TYPE_SUPPORTED(_Machine) \
  108. ((_Machine) == EFI_IMAGE_MACHINE_X64)
  109. #else
  110. #error Unsupported Architecture
  111. #endif
  112. //
  113. // ---------------------------------------------------------------- Definitions
  114. //
  115. //
  116. // Define some inherent constants.
  117. //
  118. #ifndef TRUE
  119. #define TRUE 1
  120. #endif
  121. #ifndef FALSE
  122. #define FALSE 0
  123. #endif
  124. #ifndef NULL
  125. #define NULL ((VOID *)0)
  126. #endif
  127. #define CONST const
  128. #define STATIC static
  129. #ifndef VOID
  130. #define VOID void
  131. #endif
  132. #define PACKED __attribute__((__packed__))
  133. #define __USED __attribute__((used))
  134. //
  135. // Define standard type limits.
  136. //
  137. #define MAX_INT8 ((INT8)0x7F)
  138. #define MAX_UINT8 ((UINT8)0xFF)
  139. #define MAX_INT16 ((INT16)0x7FFF)
  140. #define MAX_UINT16 ((UINT16)0xFFFF)
  141. #define MAX_INT32 ((INT32)0x7FFFFFFF)
  142. #define MAX_UINT32 ((UINT32)0xFFFFFFFF)
  143. #define MAX_INT64 ((INT64)0x7FFFFFFFFFFFFFFFULL)
  144. #define MAX_UINT64 ((UINT64)0xFFFFFFFFFFFFFFFFULL)
  145. //
  146. // Define the success status code.
  147. //
  148. #define RETURN_SUCCESS 0
  149. //
  150. // Define internal status codes.
  151. //
  152. //
  153. // The image failed to load.
  154. //
  155. #define RETURN_LOAD_ERROR ENCODE_ERROR(1)
  156. //
  157. // The parameter was incorrect.
  158. //
  159. #define RETURN_INVALID_PARAMETER ENCODE_ERROR(2)
  160. //
  161. // The operation is not supported.
  162. //
  163. #define RETURN_UNSUPPORTED ENCODE_ERROR(3)
  164. //
  165. // The buffer was not the proper size for the request.
  166. //
  167. #define RETURN_BAD_BUFFER_SIZE ENCODE_ERROR(4)
  168. //
  169. // The buffer was not large enough to hold the requested data.
  170. // The required buffer size is returned in the appropriate parameter when this
  171. // error occurs.
  172. //
  173. #define RETURN_BUFFER_TOO_SMALL ENCODE_ERROR(5)
  174. //
  175. // There is no data pending upon return.
  176. //
  177. #define RETURN_NOT_READY ENCODE_ERROR(6)
  178. //
  179. // The physical device reported an error while attempting the operation.
  180. //
  181. #define RETURN_DEVICE_ERROR ENCODE_ERROR(7)
  182. //
  183. // The device can not be written to.
  184. //
  185. #define RETURN_WRITE_PROTECTED ENCODE_ERROR(8)
  186. //
  187. // The resource has run out.
  188. //
  189. #define RETURN_OUT_OF_RESOURCES ENCODE_ERROR(9)
  190. //
  191. // An inconsistency was detected on the file system causing the operation to
  192. // fail.
  193. //
  194. #define RETURN_VOLUME_CORRUPTED ENCODE_ERROR(10)
  195. //
  196. // There is no more space on the file system.
  197. //
  198. #define RETURN_VOLUME_FULL ENCODE_ERROR(11)
  199. //
  200. // The device does not contain any medium to perform the operation.
  201. //
  202. #define RETURN_NO_MEDIA ENCODE_ERROR(12)
  203. //
  204. // The medium in the device has changed since the last access.
  205. //
  206. #define RETURN_MEDIA_CHANGED ENCODE_ERROR(13)
  207. //
  208. // The item was not found.
  209. //
  210. #define RETURN_NOT_FOUND ENCODE_ERROR(14)
  211. //
  212. // Access was denied.
  213. //
  214. #define RETURN_ACCESS_DENIED ENCODE_ERROR(15)
  215. //
  216. // The server was not found or did not respond to the request.
  217. //
  218. #define RETURN_NO_RESPONSE ENCODE_ERROR(16)
  219. //
  220. // A mapping to the device does not exist.
  221. //
  222. #define RETURN_NO_MAPPING ENCODE_ERROR(17)
  223. //
  224. // A timeout time expired.
  225. //
  226. #define RETURN_TIMEOUT ENCODE_ERROR(18)
  227. //
  228. // The protocol has not been started.
  229. //
  230. #define RETURN_NOT_STARTED ENCODE_ERROR(19)
  231. //
  232. // The protocol has already been started.
  233. //
  234. #define RETURN_ALREADY_STARTED ENCODE_ERROR(20)
  235. //
  236. // The operation was aborted.
  237. //
  238. #define RETURN_ABORTED ENCODE_ERROR(21)
  239. //
  240. // An ICMP error occurred during the network operation.
  241. //
  242. #define RETURN_ICMP_ERROR ENCODE_ERROR(22)
  243. //
  244. // A TFTP error occurred during the network operation.
  245. //
  246. #define RETURN_TFTP_ERROR ENCODE_ERROR(23)
  247. //
  248. // A protocol error occurred during the network operation.
  249. //
  250. #define RETURN_PROTOCOL_ERROR ENCODE_ERROR(24)
  251. //
  252. // A function encountered an internal version that was incompatible with a
  253. // version requested by the caller.
  254. //
  255. #define RETURN_INCOMPATIBLE_VERSION ENCODE_ERROR(25)
  256. //
  257. // The function was not performed due to a security violation.
  258. //
  259. #define RETURN_SECURITY_VIOLATION ENCODE_ERROR(26)
  260. //
  261. // A CRC error was detected.
  262. //
  263. #define RETURN_CRC_ERROR ENCODE_ERROR(27)
  264. //
  265. // The beginning or end of media was reached.
  266. //
  267. #define RETURN_END_OF_MEDIA ENCODE_ERROR(28)
  268. //
  269. // The end of the file was reached.
  270. //
  271. #define RETURN_END_OF_FILE ENCODE_ERROR(31)
  272. //
  273. // The language specified was invalid.
  274. //
  275. #define RETURN_INVALID_LANGUAGE ENCODE_ERROR(32)
  276. //
  277. // The security status of the data is unknown or compromised and the data must
  278. // be updated or replaced to restore a valid security status.
  279. //
  280. #define RETURN_COMPROMISED_DATA ENCODE_ERROR(33)
  281. //
  282. // The string contained one or more characters that the device could not render
  283. // and were skipped.
  284. //
  285. #define RETURN_WARN_UNKNOWN_GLYPH ENCODE_WARNING(1)
  286. //
  287. // The handle was closed, but the file was not deleted.
  288. //
  289. #define RETURN_WARN_DELETE_FAILURE ENCODE_WARNING(2)
  290. //
  291. // The handle was closed, but the data to the file was not flushed properly.
  292. //
  293. #define RETURN_WARN_WRITE_FAILURE ENCODE_WARNING(3)
  294. //
  295. // The resulting buffer was too small, and the data was truncated to the buffer
  296. // size.
  297. //
  298. #define RETURN_WARN_BUFFER_TOO_SMALL ENCODE_WARNING(4)
  299. //
  300. // The data has not been updated within the timeframe set by local policy for
  301. // this type of data.
  302. //
  303. #define RETURN_WARN_STALE_DATA ENCODE_WARNING(5)
  304. //
  305. // Define the status codes in the UEFI spec.
  306. //
  307. #define EFI_SUCCESS RETURN_SUCCESS
  308. #define EFI_LOAD_ERROR RETURN_LOAD_ERROR
  309. #define EFI_INVALID_PARAMETER RETURN_INVALID_PARAMETER
  310. #define EFI_UNSUPPORTED RETURN_UNSUPPORTED
  311. #define EFI_BAD_BUFFER_SIZE RETURN_BAD_BUFFER_SIZE
  312. #define EFI_BUFFER_TOO_SMALL RETURN_BUFFER_TOO_SMALL
  313. #define EFI_NOT_READY RETURN_NOT_READY
  314. #define EFI_DEVICE_ERROR RETURN_DEVICE_ERROR
  315. #define EFI_WRITE_PROTECTED RETURN_WRITE_PROTECTED
  316. #define EFI_OUT_OF_RESOURCES RETURN_OUT_OF_RESOURCES
  317. #define EFI_VOLUME_CORRUPTED RETURN_VOLUME_CORRUPTED
  318. #define EFI_VOLUME_FULL RETURN_VOLUME_FULL
  319. #define EFI_NO_MEDIA RETURN_NO_MEDIA
  320. #define EFI_MEDIA_CHANGED RETURN_MEDIA_CHANGED
  321. #define EFI_NOT_FOUND RETURN_NOT_FOUND
  322. #define EFI_ACCESS_DENIED RETURN_ACCESS_DENIED
  323. #define EFI_NO_RESPONSE RETURN_NO_RESPONSE
  324. #define EFI_NO_MAPPING RETURN_NO_MAPPING
  325. #define EFI_TIMEOUT RETURN_TIMEOUT
  326. #define EFI_NOT_STARTED RETURN_NOT_STARTED
  327. #define EFI_ALREADY_STARTED RETURN_ALREADY_STARTED
  328. #define EFI_ABORTED RETURN_ABORTED
  329. #define EFI_ICMP_ERROR RETURN_ICMP_ERROR
  330. #define EFI_TFTP_ERROR RETURN_TFTP_ERROR
  331. #define EFI_PROTOCOL_ERROR RETURN_PROTOCOL_ERROR
  332. #define EFI_INCOMPATIBLE_VERSION RETURN_INCOMPATIBLE_VERSION
  333. #define EFI_SECURITY_VIOLATION RETURN_SECURITY_VIOLATION
  334. #define EFI_CRC_ERROR RETURN_CRC_ERROR
  335. #define EFI_END_OF_MEDIA RETURN_END_OF_MEDIA
  336. #define EFI_END_OF_FILE RETURN_END_OF_FILE
  337. #define EFI_INVALID_LANGUAGE RETURN_INVALID_LANGUAGE
  338. #define EFI_COMPROMISED_DATA RETURN_COMPROMISED_DATA
  339. #define EFI_WARN_UNKNOWN_GLYPH RETURN_WARN_UNKNOWN_GLYPH
  340. #define EFI_WARN_DELETE_FAILURE RETURN_WARN_DELETE_FAILURE
  341. #define EFI_WARN_WRITE_FAILURE RETURN_WARN_WRITE_FAILURE
  342. #define EFI_WARN_BUFFER_TOO_SMALL RETURN_WARN_BUFFER_TOO_SMALL
  343. #define EFI_WARN_STALE_DATA RETURN_WARN_STALE_DATA
  344. //
  345. // Define additional ICMP error codes.
  346. //
  347. #define EFI_NETWORK_UNREACHABLE EFIERR(100)
  348. #define EFI_HOST_UNREACHABLE EFIERR(101)
  349. #define EFI_PROTOCOL_UNREACHABLE EFIERR(102)
  350. #define EFI_PORT_UNREACHABLE EFIERR(103)
  351. //
  352. // Define additional TCP error codes.
  353. //
  354. #define EFI_CONNECTION_FIN EFIERR(104)
  355. #define EFI_CONNECTION_RESET EFIERR(105)
  356. #define EFI_CONNECTION_REFUSED EFIERR(106)
  357. //
  358. // Define the page size of EFI pages, which are doled out by the EFI page
  359. // allocator. This is not necessarily the same as the processor page size.
  360. //
  361. #define EFI_PAGE_SIZE 0x1000
  362. #define EFI_PAGE_MASK 0xFFF
  363. #define EFI_PAGE_SHIFT 12
  364. //
  365. // Define PE32+ Machine Types.
  366. //
  367. #define EFI_IMAGE_MACHINE_IA32 0x014C
  368. #define EFI_IMAGE_MACHINE_IA64 0x0200
  369. #define EFI_IMAGE_MACHINE_EBC 0x0EBC
  370. #define EFI_IMAGE_MACHINE_X64 0x8664
  371. #define EFI_IMAGE_MACHINE_ARMTHUMB_MIXED 0x01C2
  372. #define EFI_IMAGE_MACHINE_AARCH64 0xAA64
  373. //
  374. // ------------------------------------------------------ Data Type Definitions
  375. //
  376. //
  377. // Define the vararg list type.
  378. //
  379. typedef __builtin_va_list VA_LIST;
  380. typedef struct {
  381. UINT32 Data1;
  382. UINT16 Data2;
  383. UINT16 Data3;
  384. UINT8 Data4[8];
  385. } GUID;
  386. //
  387. // Define the type for a physical memory address.
  388. //
  389. #ifndef __MINOCA_TYPES_H
  390. typedef UINT64 PHYSICAL_ADDRESS;
  391. #endif
  392. //
  393. // Define the internal type for a standard return status.
  394. //
  395. typedef UINTN RETURN_STATUS;
  396. //
  397. // External types defined by the UEFI specification.
  398. //
  399. //
  400. // 128-bit buffer containing a unique identifier value.
  401. //
  402. typedef GUID EFI_GUID;
  403. //
  404. // Function return status for EFI API.
  405. //
  406. typedef RETURN_STATUS EFI_STATUS;
  407. //
  408. // A collection of related interfaces.
  409. //
  410. typedef VOID *EFI_HANDLE;
  411. //
  412. // Handle to an event structure.
  413. //
  414. typedef VOID *EFI_EVENT;
  415. //
  416. // Task priority level.
  417. //
  418. typedef UINTN EFI_TPL;
  419. //
  420. // Logical block address.
  421. //
  422. typedef UINT64 EFI_LBA;
  423. //
  424. // 64-bit physical memory address.
  425. //
  426. typedef UINT64 EFI_PHYSICAL_ADDRESS;
  427. //
  428. // 64-bit virtual memory address.
  429. //
  430. typedef UINT64 EFI_VIRTUAL_ADDRESS;
  431. /*++
  432. Structure Description:
  433. This structure defines a point in calendar time.
  434. Members:
  435. Year - Stores the year. Valid values are between 1900 and 9999, inclusive.
  436. Month - Stores the month. Valid values are between 1 and 12, inclusive.
  437. Day - Stores the day of the month. Valid values are between 1 and 31,
  438. inclusive (well, sometimes less depending on the month).
  439. Hour - Stores the hour of the day. Valid values are between 0 and 23,
  440. inclusive.
  441. Minute - Stores the minute of the hour. Valid values are between 0 and 59,
  442. inclusive.
  443. Second - Stores the second of the minute. Valid values are between 0 and
  444. 59, inclusive. Leap seconds are not accounted for.
  445. Pad1 - Stores a reserved byte used to pad the structure.
  446. Nanosecond - Stores the nanosecond of the second. Valid values are between
  447. 0 and 999999999, inclusive.
  448. TimeZone - Stores the offset from UTC this time is relative to. Valid
  449. values are between -1440 to 1440, inclusive, or 2047.
  450. Daylight - Stores daylight saving flags. See EFI_TIME_* definitions.
  451. Pad2 - Stores another reserved byte used to pad the structure.
  452. --*/
  453. typedef struct {
  454. UINT16 Year;
  455. UINT8 Month;
  456. UINT8 Day;
  457. UINT8 Hour;
  458. UINT8 Minute;
  459. UINT8 Second;
  460. UINT8 Pad1;
  461. UINT32 Nanosecond;
  462. INT16 TimeZone;
  463. UINT8 Daylight;
  464. UINT8 Pad2;
  465. } EFI_TIME;
  466. /*++
  467. Structure Description:
  468. This structure defines an Internet Protocol v4 address.
  469. Members:
  470. Addr - Stores the 4-byte address.
  471. --*/
  472. typedef struct {
  473. UINT8 Addr[4];
  474. } EFI_IPv4_ADDRESS;
  475. /*++
  476. Structure Description:
  477. This structure defines an Internet Protocol v6 address.
  478. Members:
  479. Addr - Stores the 16-byte address.
  480. --*/
  481. typedef struct {
  482. UINT8 Addr[16];
  483. } EFI_IPv6_ADDRESS;
  484. /*++
  485. Structure Description:
  486. This structure defines an Media Access Control address.
  487. Members:
  488. Addr - Stores the 32-byte address.
  489. --*/
  490. typedef struct {
  491. UINT8 Addr[32];
  492. } EFI_MAC_ADDRESS;
  493. /*++
  494. Structure Description:
  495. This union defines a storage unit that any type of network address can fit
  496. into.
  497. Members:
  498. Addr - Stores the data, used to create a minimum size and alignment.
  499. v4 - Stores the IPv4 address.
  500. v6 - Stores the IPv6 address.
  501. --*/
  502. typedef union {
  503. UINT32 Addr[4];
  504. EFI_IPv4_ADDRESS v4;
  505. EFI_IPv6_ADDRESS v6;
  506. } EFI_IP_ADDRESS;
  507. //
  508. // -------------------------------------------------------------------- Globals
  509. //
  510. //
  511. // -------------------------------------------------------- Function Prototypes
  512. //