sandboxfs.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright (c) 2012, Google Inc.
  4. */
  5. #include <common.h>
  6. #include <fs.h>
  7. #include <malloc.h>
  8. #include <os.h>
  9. int sandbox_fs_set_blk_dev(struct blk_desc *rbdd, struct disk_partition *info)
  10. {
  11. /*
  12. * Only accept a NULL struct blk_desc for the sandbox, which is when
  13. * hostfs interface is used
  14. */
  15. return rbdd != NULL;
  16. }
  17. int sandbox_fs_read_at(const char *filename, loff_t pos, void *buffer,
  18. loff_t maxsize, loff_t *actread)
  19. {
  20. loff_t size;
  21. int fd, ret;
  22. fd = os_open(filename, OS_O_RDONLY);
  23. if (fd < 0)
  24. return fd;
  25. ret = os_lseek(fd, pos, OS_SEEK_SET);
  26. if (ret == -1) {
  27. os_close(fd);
  28. return ret;
  29. }
  30. if (!maxsize) {
  31. ret = os_get_filesize(filename, &size);
  32. if (ret) {
  33. os_close(fd);
  34. return ret;
  35. }
  36. maxsize = size;
  37. }
  38. size = os_read(fd, buffer, maxsize);
  39. os_close(fd);
  40. if (size < 0) {
  41. ret = -1;
  42. } else {
  43. ret = 0;
  44. *actread = size;
  45. }
  46. return ret;
  47. }
  48. int sandbox_fs_write_at(const char *filename, loff_t pos, void *buffer,
  49. loff_t towrite, loff_t *actwrite)
  50. {
  51. ssize_t size;
  52. int fd, ret;
  53. fd = os_open(filename, OS_O_RDWR | OS_O_CREAT);
  54. if (fd < 0)
  55. return fd;
  56. ret = os_lseek(fd, pos, OS_SEEK_SET);
  57. if (ret == -1) {
  58. os_close(fd);
  59. return ret;
  60. }
  61. size = os_write(fd, buffer, towrite);
  62. os_close(fd);
  63. if (size == -1) {
  64. ret = -1;
  65. } else {
  66. ret = 0;
  67. *actwrite = size;
  68. }
  69. return ret;
  70. }
  71. int sandbox_fs_ls(const char *dirname)
  72. {
  73. struct os_dirent_node *head, *node;
  74. int ret;
  75. ret = os_dirent_ls(dirname, &head);
  76. if (ret)
  77. goto out;
  78. for (node = head; node; node = node->next) {
  79. printf("%s %10lu %s\n", os_dirent_get_typename(node->type),
  80. node->size, node->name);
  81. }
  82. out:
  83. os_dirent_free(head);
  84. return ret;
  85. }
  86. int sandbox_fs_exists(const char *filename)
  87. {
  88. loff_t size;
  89. int ret;
  90. ret = os_get_filesize(filename, &size);
  91. return ret == 0;
  92. }
  93. int sandbox_fs_size(const char *filename, loff_t *size)
  94. {
  95. return os_get_filesize(filename, size);
  96. }
  97. void sandbox_fs_close(void)
  98. {
  99. }
  100. int fs_read_sandbox(const char *filename, void *buf, loff_t offset, loff_t len,
  101. loff_t *actread)
  102. {
  103. int ret;
  104. ret = sandbox_fs_read_at(filename, offset, buf, len, actread);
  105. if (ret)
  106. printf("** Unable to read file %s **\n", filename);
  107. return ret;
  108. }
  109. int fs_write_sandbox(const char *filename, void *buf, loff_t offset,
  110. loff_t len, loff_t *actwrite)
  111. {
  112. int ret;
  113. ret = sandbox_fs_write_at(filename, offset, buf, len, actwrite);
  114. if (ret)
  115. printf("** Unable to write file %s **\n", filename);
  116. return ret;
  117. }