Subversion Repositories QNX 8.QNX8 IFS tool

Rev

Rev 22 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  1. #ifndef IFSFILE_H
  2. #define IFSFILE_H
  3.  
  4.  
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8.  
  9.  
  10. // standard C includes
  11. #include <stdint.h>
  12. #include <stdlib.h>
  13.  
  14.  
  15. // compiler-specific glue
  16. #ifdef _WIN32
  17. #ifndef __BYTE_ORDER__
  18. #define __ORDER_BIG_ENDIAN__    4321
  19. #define __ORDER_LITTLE_ENDIAN__ 1234
  20. #define __BYTE_ORDER__ __ORDER_LITTLE_ENDIAN__ // all Windows machines are little endian
  21. #endif // !__BYTE_ORDER__
  22. #ifndef __builtin_bswap64
  23. #ifdef _MSC_VER
  24. #define __builtin_bswap64(x) _byteswap_uint64 ((unsigned long long) (x))
  25. #endif // _MSC_VER
  26. #endif // !__builtin_bswap64
  27. #ifndef __builtin_bswap32
  28. #ifdef _MSC_VER
  29. #define __builtin_bswap32(x) _byteswap_ulong ((unsigned long) (x))
  30. #endif // _MSC_VER
  31. #endif // !__builtin_bswap32
  32. #ifndef __builtin_bswap16
  33. #ifdef _MSC_VER
  34. #define __builtin_bswap16(x) _byteswap_ushort ((unsigned short) (x))
  35. #endif // _MSC_VER
  36. #endif // !__builtin_bswap32
  37. #endif // _WIN32
  38. #ifdef _MSC_VER
  39. #define START_OF_PACKED_STRUCT() __pragma(pack(push)) __pragma(pack(1))
  40. #define END_OF_PACKED_STRUCT() __pragma(pack(pop))
  41. #define PACKED(thing) thing
  42. #else // !_MSC_VER
  43. #define START_OF_PACKED_STRUCT()
  44. #define END_OF_PACKED_STRUCT()
  45. #define PACKED(thing) thing __attribute__((packed))
  46. #endif // _MSC_VER
  47.  
  48.  
  49. // bitmapped flags used in the flags1 member of the startup header
  50. #define STARTUP_HDR_FLAGS1_VIRTUAL        (1 << 0)
  51. #define STARTUP_HDR_FLAGS1_BIGENDIAN      (1 << 1)
  52. //#define STARTUP_HDR_FLAGS1_COMPRESS_MASK  0x1c
  53. //#define STARTUP_HDR_FLAGS1_COMPRESS_SHIFT 0x02
  54. //#define STARTUP_HDR_FLAGS1_COMPRESS_NONE  0x00
  55. //#define STARTUP_HDR_FLAGS1_COMPRESS_ZLIB  0x04
  56. //#define STARTUP_HDR_FLAGS1_COMPRESS_LZO   0x08
  57. //#define STARTUP_HDR_FLAGS1_COMPRESS_UCL   0x0c
  58. #define STARTUP_HDR_FLAGS1_TRAILER_V2     (1 << 5) // if set, then a struct startup_trailer_v2 follows the startup. If the image is compressed, then the compressed imagefs is followed by a struct image_trailer_v2
  59.  
  60.  
  61. // bitmapped flags used in the flags member of the image header
  62. #define IMAGE_FLAGS_BIGENDIAN  (1 << 0) // header, trailer, dirents in big-endian format
  63. #define IMAGE_FLAGS_READONLY   (1 << 1) // do not try to write to image (rom/flash)
  64. #define IMAGE_FLAGS_INO_BITS   (1 << 2) // inode bits valid
  65. #define IMAGE_FLAGS_SORTED     (1 << 3) // dirent section is sorted (by pathname)
  66. #define IMAGE_FLAGS_TRAILER_V2 (1 << 4) // image uses struct image_trailer_v2
  67.  
  68.  
  69. // bitmapped flags superposed to a filesystem entry's inode number
  70. #define IFS_INO_PROCESSED_ELF 0x80000000
  71. #define IFS_INO_RUNONCE_ELF   0x40000000
  72. #define IFS_INO_BOOTSTRAP_EXE 0x20000000
  73.  
  74.  
  75. #if 0 // TODO: startup script compiler. Someday.
  76. #define SCRIPT_FLAGS_EXTSCHED   0x01
  77. #define SCRIPT_FLAGS_SESSION    0x02
  78. #define SCRIPT_FLAGS_SCHED_SET  0x04
  79. #define SCRIPT_FLAGS_CPU_SET    0x08
  80. #define SCRIPT_FLAGS_BACKGROUND 0x20
  81. #define SCRIPT_FLAGS_KDEBUG     0x40
  82.  
  83. #define SCRIPT_POLICY_NOCHANGE 0
  84. #define SCRIPT_POLICY_FIFO     1
  85. #define SCRIPT_POLICY_RR       2
  86. #define SCRIPT_POLICY_OTHER    3
  87.  
  88. #define SCRIPT_TYPE_EXTERNAL        0
  89. #define SCRIPT_TYPE_WAITFOR         1
  90. #define SCRIPT_TYPE_REOPEN          2
  91. #define SCRIPT_TYPE_DISPLAY_MSG     3
  92. #define SCRIPT_TYPE_PROCMGR_SYMLINK 4
  93. #define SCRIPT_TYPE_EXTSCHED_APS    5
  94.  
  95. #define SCRIPT_CHECKS_MS 100
  96.  
  97. #define SCRIPT_SCHED_EXT_NONE 0
  98. #define SCRIPT_SCHED_EXT_APS  1
  99.  
  100. #define SCRIPT_APS_SYSTEM_PARTITION_ID   0
  101. #define SCRIPT_APS_SYSTEM_PARTITION_NAME "System"
  102. #define SCRIPT_APS_PARTITION_NAME_LENGTH 15
  103. #define SCRIPT_APS_MAX_PARTITIONS        8
  104.  
  105.  
  106. START_OF_PACKED_STRUCT () // we need byte-alignment for this struct
  107. typedef PACKED (struct) bootscriptcmd_header_s
  108. {
  109.    uint16_t size; // size of cmd entry
  110.    uint8_t type;
  111.    uint8_t spare;
  112. } bootscriptcmd_header_t;
  113. END_OF_PACKED_STRUCT () // restore default alignment
  114.  
  115.  
  116. START_OF_PACKED_STRUCT () // we need byte-alignment for this struct
  117. typedef union bootscriptcmd_s
  118. {
  119.    PACKED (struct) script_external
  120.    {
  121.       bootscriptcmd_header_t hdr;
  122.       uint8_t cpu; // CPU (turn into runmask)
  123.       uint8_t flags;
  124.       union script_external_extsched
  125.       {
  126.          uint8_t reserved[2];
  127.          PACKED (struct)
  128.          {
  129.             uint8_t id;
  130.             uint8_t reserved[1];
  131.          } aps;
  132.       } extsched; // extended scheduler
  133.       uint8_t policy; // POLICY_FIFO, POLICY_RR, ...
  134.       uint8_t priority; // priority to run cmd at
  135.       uint8_t argc; // # of args
  136.       uint8_t envc; // # of environment entries
  137.       char args[0]; // executable, argv, envp (null padded to 32-bit align)
  138.    } external;
  139.    PACKED (struct) script_waitfor_reopen
  140.    {
  141.       bootscriptcmd_header_t hdr;
  142.       uint16_t checks;
  143.       char fname[0]; // char fname[] (null padded to 32-bit align)
  144.    } waitfor_reopen;
  145.    PACKED (struct) script_display_msg
  146.    {
  147.       bootscriptcmd_header_t hdr;
  148.       char msg[0]; // char msg[] (null padded to 32-bit align)
  149.    } display_msg;
  150.    PACKED (struct) script_procmgr_symlink
  151.    {
  152.       bootscriptcmd_header_t hdr;
  153.       char src_dest[0]; // <src_name>, '\0', <dest_name> '\0' (null padded to 32-bit align)
  154.    } procmgr_symlink;
  155.    PACKED (struct) script_extsched_aps
  156.    {
  157.       bootscriptcmd_header_t hdr;
  158.       uint8_t parent;
  159.       uint8_t budget;
  160.       uint16_t critical;
  161.       uint8_t id;
  162.       char pname[0]; // char pname[] (null padded to 32-bit align)
  163.    } extsched_aps;
  164. } bootscriptcmd_t;
  165. END_OF_PACKED_STRUCT () // restore default alignment
  166. #endif // 0
  167.  
  168.  
  169. START_OF_PACKED_STRUCT () // we need byte-alignment for this struct
  170. typedef PACKED (struct) fsentry_s
  171. {
  172.    PACKED (struct) fsentry_header_s
  173.    {
  174.       uint16_t size; // size of dirent
  175.       uint16_t extattr_offset; // if zero, no extattr data
  176.       uint32_t ino; // if zero, skip entry
  177.       uint32_t mode; // mode and perms of entry
  178.       uint32_t gid;
  179.       uint32_t uid;
  180.       uint32_t mtime;
  181.    } header;
  182.    PACKED (union) fsentry_specific_u
  183.    {
  184.       PACKED (struct) fsentry_file_s // when (mode & S_IFMT) == S_IFREG
  185.       {
  186.          uint32_t offset; // offset from header
  187.          uint32_t size;
  188.          char *path; // null terminated path (no leading slash)
  189.          uint8_t *UNSAVED_databuf; // file data blob buffer (convenience pointer, NOT SAVED IN THE IFS)
  190.       } file;
  191.       PACKED (struct) fsentry_dir_s // when (mode & S_IFMT) == S_IFDIR
  192.       {
  193.          char *path; // null terminated path (no leading slash)
  194.       } dir;
  195.       PACKED (struct) fsentry_symlink_s // when (mode & S_IFMT) == S_IFLNK
  196.       {
  197.          uint16_t sym_offset; // offset to 'contents' from 'path'
  198.          uint16_t sym_size; // strlen (contents)
  199.          char *path; // null terminated path (no leading slash)
  200.          char *contents; // null terminated symlink contents
  201.       } symlink;
  202.       PACKED (struct) fsentry_device_s // when (mode & S_IFMT) == S_IF<CHR|BLK|FIFO|NAM|SOCK>
  203.       {
  204.          uint32_t dev;
  205.          uint32_t rdev;
  206.          char *path; // null terminated path (no leading slash)
  207.       } device;
  208.    } u;
  209.    bool UNSAVED_was_data_written; // whether this entry's data was written to the image (convenience member, NOT SAVED IN THE IFS)
  210. } fsentry_t;
  211. END_OF_PACKED_STRUCT () // restore default alignment
  212.  
  213.  
  214. START_OF_PACKED_STRUCT () // we need byte-alignment for this struct
  215. typedef PACKED (struct) startup_header_s // size 256 bytes
  216. {
  217.    // I - used by the QNX IPL
  218.    // S - used by the startup program
  219.    uint8_t signature[4];   // [I ] Header signature, "\xeb\x7e\xff\x00"
  220.    uint16_t version;       // [I ] Header version, i.e. 1
  221.    uint8_t flags1;         // [IS] Misc flags, 0x21 (= 0x20 | STARTUP_HDR_FLAGS1_VIRTUAL)
  222.    uint8_t flags2;         // [  ] No flags defined yet (0)
  223.    uint16_t header_size;   // [ S] sizeof(struct startup_header), i.e. 256
  224.    uint16_t machine;       // [IS] Machine type from elfdefinitions.h, i.e. 0x003E --> _ELF_DEFINE_EM(EM_X86_64, 62, "AMD x86-64 architecture")
  225.    uint32_t startup_vaddr; // [I ] Virtual Address to transfer to after IPL is done, here 0x01403008 (appears in "Entry" column for "startup.*")
  226.    uint32_t paddr_bias;    // [ S] Value to add to physical address to get a value to put into a pointer and indirected through, here 0 (no indirections)
  227.    uint32_t image_paddr;   // [IS] Physical address of image, here 0x01400f30 (appears in "Offset" column for "startup-header" which is the first entry/start of file)
  228.    uint32_t ram_paddr;     // [IS] Physical address of RAM to copy image to (startup_size bytes copied), here 0x01400f30 (same as above)
  229.    uint32_t ram_size;      // [ S] Amount of RAM used by the startup program and executables contained in the file system, here 0x00cd6128 i.e. 13 459 752 dec. which is 13 Mb. i.e. IFS file size minus 0x9eee
  230.    uint32_t startup_size;  // [I ] Size of startup (never compressed), here 0x02f148 or 192 840 bytes
  231.    uint32_t stored_size;   // [I ] Size of entire image, here 0x00cd6128 (same as ram_size)
  232.    uint32_t imagefs_paddr; // [IS] Set by IPL to where the imagefs is when startup runs (0)
  233.    uint32_t imagefs_size;  // [ S] Size of uncompressed imagefs, here 0x00ca6fe0 or 13 266 912 bytes
  234.    uint16_t preboot_size;  // [I ] Size of loaded before header, here 0xf30 or 3888 bytes (size of "bios.boot" file))
  235.    uint16_t zero0;         // [  ] Zeros
  236.    uint32_t zero[1];       // [  ] Zeros
  237.    uint64_t addr_off;      // [ S] Offset to add to startup_vaddr, image_paddr, ram_paddr, and imagefs_paddr members, here zero (0)
  238.    uint32_t info[48];      // [IS] Array of startup_info* structures (zero filled)
  239. } startup_header_t;
  240. END_OF_PACKED_STRUCT () // restore default alignment
  241.  
  242.  
  243. START_OF_PACKED_STRUCT () // we need byte-alignment for this struct
  244. typedef PACKED (struct) startup_trailer_s
  245. {
  246.    uint32_t cksum; // checksum from start of header to start of trailer
  247. } startup_trailer_v1_t;
  248. END_OF_PACKED_STRUCT () // restore default alignment
  249.  
  250.  
  251. // NOTE: The checksums in this trailer will only be valid prior to entering startup.
  252. // Because the startup binary is executed in-place, its data segment will change once the program is running.
  253. // Hence, any checksum validation would need to be done by the boot loader / IFS.
  254. START_OF_PACKED_STRUCT () // we need byte-alignment for this struct
  255. typedef PACKED (struct) startup_trailer_v2_s
  256. {
  257.    uint8_t sha512[64]; // SHA512 from start of header to start of trailer
  258.    uint32_t cksum; // checksum from start of header to start of this member
  259. } startup_trailer_v2_t;
  260. END_OF_PACKED_STRUCT () // restore default alignment
  261.  
  262.  
  263. START_OF_PACKED_STRUCT () // we need byte-alignment for this struct
  264. typedef PACKED (struct) image_header_s
  265. {
  266.    uint8_t signature[7]; // image filesystem signature, i.e. "imagefs"
  267.    uint8_t flags; // endian neutral flags, 0x1c
  268.    uint32_t image_size; // size from start of header to end of trailer (here 0xca6fe0 or 13 266 912)
  269.    uint32_t hdr_dir_size; // size from start of header to last dirent (here 0x12b8 or 4792)
  270.    uint32_t dir_offset; // offset from start of header to start of first dirent (here 0x5c or 92)
  271.    uint32_t boot_ino[4]; // inode of files for bootstrap pgms (here 0xa0000002, 0, 0, 0)
  272.    uint32_t script_ino; // inode of file for script (here 3)
  273.    uint32_t chain_paddr; // offset to next filesystem signature (0)
  274.    uint32_t spare[10]; // zerofill
  275.    uint32_t mountflags; // default _MOUNT_* from sys/iomsg.h (0)
  276.    char mountpoint[4]; // default mountpoint for image ("/" + "\0\0\0")
  277. } image_header_t;
  278. END_OF_PACKED_STRUCT () // restore default alignment
  279.  
  280.  
  281. START_OF_PACKED_STRUCT () // we need byte-alignment for this struct
  282. typedef PACKED (struct) image_trailer_v1_s
  283. {
  284.    uint32_t cksum; // checksum from start of header to start of trailer
  285. } image_trailer_v1_t; // NOTE: this is the same structure as startup_trailer_v1_t
  286. END_OF_PACKED_STRUCT () // restore default alignment
  287.  
  288.  
  289. // NOTE: the checksums in this trailer will only be valid until the first non-startup bootstrap binary (e.g., startup-verifier, procnto, ...) is invoked.
  290. // Because bootstrap binaries execute in-place, their data segments will change once the programs are running.
  291. // Hence, any checksum validation would need to be done either by the boot loader / IFS or by the startup.
  292. START_OF_PACKED_STRUCT () // we need byte-alignment for this struct
  293. typedef PACKED (struct) image_trailer_v2_s
  294. {
  295.    uint8_t sha512[64]; // SHA512 from start of image header to start of trailer
  296.    uint32_t cksum; // checksum from start of header to start of this member
  297. } image_trailer_v2_t; // NOTE: this is the same structure as startup_trailer_v2_t
  298. END_OF_PACKED_STRUCT () // restore default alignment
  299.  
  300.  
  301. // undefine the helpers we no longer need
  302. #undef START_OF_PACKED_STRUCT
  303. #undef END_OF_PACKED_STRUCT
  304. #undef PACKED
  305.  
  306.  
  307. #ifdef __cplusplus
  308. }
  309. #endif
  310.  
  311.  
  312. #endif // IFSFILE_H
  313.