sandboxfs.c 2.4 KB

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