cpio.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * mkbootimg/cpio.c
  3. *
  4. * Copyright (C) 2017 - 2021 bzt (bztsrc@gitlab)
  5. *
  6. * Permission is hereby granted, free of charge, to any person
  7. * obtaining a copy of this software and associated documentation
  8. * files (the "Software"), to deal in the Software without
  9. * restriction, including without limitation the rights to use, copy,
  10. * modify, merge, publish, distribute, sublicense, and/or sell copies
  11. * of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be
  15. * included in all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  20. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  21. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  22. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  24. * DEALINGS IN THE SOFTWARE.
  25. *
  26. * This file is part of the BOOTBOOT Protocol package.
  27. * @brief CPIO initrd driver
  28. * See https://en.wikipedia.org/wiki/Cpio
  29. *
  30. */
  31. #include "main.h"
  32. void cpio_open(gpt_t *gpt_entry)
  33. {
  34. if(gpt_entry) { fprintf(stderr,"mkbootimg: partition #%d %s cpio.\r\n", fs_no, lang[ERR_INITRDTYPE]); exit(1); }
  35. }
  36. void cpio_add(struct stat *st, char *name, unsigned char *content, int size)
  37. {
  38. unsigned char *end;
  39. int n = strlen(name);
  40. if(!S_ISREG(st->st_mode) && !S_ISDIR(st->st_mode) && !S_ISLNK(st->st_mode)) return;
  41. fs_base = realloc(fs_base, fs_len + 76 + n + 1 + size);
  42. if(!fs_base) { fprintf(stderr,"mkbootimg: %s\r\n", lang[ERR_MEM]); exit(1); }
  43. end = fs_base + fs_len;
  44. end += sprintf((char*)end, "070707000000000000%06o00000000000000000000000000000000000%06o%011o%s",
  45. st->st_mode & 0777777,n+1,size,name);
  46. *end++ = 0;
  47. if(content && size) memcpy(end, content, size);
  48. fs_len += 76 + n + 1 + size;
  49. }
  50. void cpio_close()
  51. {
  52. char *end;
  53. fs_base = realloc(fs_base, fs_len + 76 + 11 + 512);
  54. if(!fs_base) { fprintf(stderr,"mkbootimg: %s\r\n", lang[ERR_MEM]); exit(1); }
  55. end = (char*)fs_base + fs_len;
  56. memset(end, 0, 76 + 11 + 512);
  57. end += sprintf(end, "07070700000000000000000000000000000000000010000000000000000%06o%011oTRAILER!!!",11,0);
  58. fs_len = ((fs_len + 88 + 511) & ~511);
  59. }