sandboxfs.c 2.5 KB

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