initrd.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * Copyright (C) 2018 bzt (bztsrc@github)
  3. *
  4. * Permission is hereby granted, free of charge, to any person
  5. * obtaining a copy of this software and associated documentation
  6. * files (the "Software"), to deal in the Software without
  7. * restriction, including without limitation the rights to use, copy,
  8. * modify, merge, publish, distribute, sublicense, and/or sell copies
  9. * of the Software, and to permit persons to whom the Software is
  10. * furnished to do so, subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  19. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  20. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  22. * DEALINGS IN THE SOFTWARE.
  23. *
  24. */
  25. #include "uart.h"
  26. // add memory compare, gcc has a built-in for that, clang needs implementation
  27. #ifdef __clang__
  28. int memcmp(void *s1, void *s2, int n)
  29. {
  30. unsigned char *a=s1,*b=s2;
  31. while(n-->0){ if(*a!=*b) { return *a-*b; } a++; b++; }
  32. return 0;
  33. }
  34. #else
  35. #define memcmp __builtin_memcmp
  36. #endif
  37. /* POSIX ustar header format */
  38. typedef struct { /* byte offset */
  39. char name[100]; /* 0 */
  40. char mode[8]; /* 100 */
  41. char uid[8]; /* 108 */
  42. char gid[8]; /* 116 */
  43. char size[12]; /* 124 */
  44. char mtime[12]; /* 136 */
  45. char chksum[8]; /* 148 */
  46. char typeflag; /* 156 */
  47. char linkname[100]; /* 157 */
  48. char magic[6]; /* 257 */
  49. char version[2]; /* 263 */
  50. char uname[32]; /* 265 */
  51. char gname[32]; /* 297 */
  52. char devmajor[8]; /* 329 */
  53. char devminor[8]; /* 337 */
  54. char prefix[167]; /* 345 */
  55. } __attribute__((packed)) tar_t;
  56. /* cpio hpodc format */
  57. typedef struct {
  58. char magic[6]; /* Magic header '070707'. */
  59. char dev[6]; /* device number. */
  60. char ino[6]; /* "i-node" number. */
  61. char mode[6]; /* Permisions. */
  62. char uid[6]; /* User ID. */
  63. char gid[6]; /* Group ID. */
  64. char nlink[6]; /* Number of hard links. */
  65. char rdev[6]; /* device major/minor number. */
  66. char mtime[11]; /* Modification time. */
  67. char namesize[6]; /* Length of filename in bytes. */
  68. char filesize[11]; /* File size. */
  69. } __attribute__((packed)) cpio_t;
  70. /**
  71. * Helper function to convert ASCII octal number into binary
  72. * s string
  73. * n number of digits
  74. */
  75. int oct2bin(char *s, int n)
  76. {
  77. int r=0;
  78. while(n-->0) {
  79. r<<=3;
  80. r+=*s++-'0';
  81. }
  82. return r;
  83. }
  84. /**
  85. * List the contents of an archive
  86. */
  87. void initrd_list(char *buf)
  88. {
  89. char *types[]={"regular", "link ", "symlnk", "chrdev", "blkdev", "dircty", "fifo ", "??? "};
  90. uart_puts("Type Offset Size Access rights\tFilename\n");
  91. // iterate on archive's contents
  92. while(!memcmp(buf+257,"ustar",5)) {
  93. // if it's an ustar archive
  94. tar_t *header=(tar_t*)buf;
  95. int fs=oct2bin(header->size,11);
  96. // print out meta information
  97. uart_puts(types[header->typeflag-'0']);
  98. uart_send(' ');
  99. uart_send(' ');
  100. uart_hex((unsigned int)((unsigned long)buf)+sizeof(tar_t));
  101. uart_send(' ');
  102. uart_hex(fs); // file size in hex
  103. uart_send(' ');
  104. uart_puts(header->mode); // access bits in octal
  105. uart_send(' ');
  106. uart_puts(header->uname); // owner
  107. uart_send('.');
  108. uart_puts(header->gname); // group
  109. uart_send('\t');
  110. uart_puts(buf); // filename
  111. if(header->typeflag=='2') {
  112. uart_puts(" -> "); // symlink target
  113. uart_puts(header->linkname);
  114. }
  115. uart_puts("\n");
  116. // jump to the next file
  117. buf+=(((fs+511)/512)+1)*512;
  118. }
  119. // if it's a cpio archive. Cpio also has a trailer entry
  120. while(!memcmp(buf,"070707",6) && memcmp(buf+sizeof(cpio_t),"TRAILER!!",9)) {
  121. cpio_t *header = (cpio_t*)buf;
  122. int ns=oct2bin(header->namesize,6);
  123. int fs=oct2bin(header->filesize,11);
  124. // print out meta information
  125. uart_hex(oct2bin(header->mode,6)); // mode (access rights + type)
  126. uart_send(' ');
  127. uart_hex((unsigned int)((unsigned long)buf)+sizeof(cpio_t)+ns);
  128. uart_send(' ');
  129. uart_hex(fs); // file size in hex
  130. uart_send(' ');
  131. uart_hex(oct2bin(header->uid,6)); // user id in hex
  132. uart_send('.');
  133. uart_hex(oct2bin(header->gid,6)); // group id in hex
  134. uart_send('\t');
  135. uart_puts(buf+sizeof(cpio_t)); // filename
  136. uart_puts("\n");
  137. // jump to the next file
  138. buf+=(sizeof(cpio_t)+ns+fs);
  139. }
  140. }