os.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * Operating System Interface
  3. *
  4. * This provides access to useful OS routines for the sandbox architecture.
  5. * They are kept in a separate file so we can include system headers.
  6. *
  7. * Copyright (c) 2011 The Chromium OS Authors.
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #ifndef __OS_H__
  11. #define __OS_H__
  12. struct sandbox_state;
  13. /**
  14. * Access to the OS read() system call
  15. *
  16. * \param fd File descriptor as returned by os_open()
  17. * \param buf Buffer to place data
  18. * \param count Number of bytes to read
  19. * \return number of bytes read, or -1 on error
  20. */
  21. ssize_t os_read(int fd, void *buf, size_t count);
  22. /**
  23. * Access to the OS read() system call with non-blocking access
  24. *
  25. * \param fd File descriptor as returned by os_open()
  26. * \param buf Buffer to place data
  27. * \param count Number of bytes to read
  28. * \return number of bytes read, or -1 on error
  29. */
  30. ssize_t os_read_no_block(int fd, void *buf, size_t count);
  31. /**
  32. * Access to the OS write() system call
  33. *
  34. * \param fd File descriptor as returned by os_open()
  35. * \param buf Buffer containing data to write
  36. * \param count Number of bytes to write
  37. * \return number of bytes written, or -1 on error
  38. */
  39. ssize_t os_write(int fd, const void *buf, size_t count);
  40. /**
  41. * Access to the OS lseek() system call
  42. *
  43. * \param fd File descriptor as returned by os_open()
  44. * \param offset File offset (based on whence)
  45. * \param whence Position offset is relative to (see below)
  46. * \return new file offset
  47. */
  48. off_t os_lseek(int fd, off_t offset, int whence);
  49. /* Defines for "whence" in os_lseek() */
  50. #define OS_SEEK_SET 0
  51. #define OS_SEEK_CUR 1
  52. #define OS_SEEK_END 2
  53. /**
  54. * Access to the OS open() system call
  55. *
  56. * \param pathname Pathname of file to open
  57. * \param flags Flags, like O_RDONLY, O_RDWR
  58. * \return file descriptor, or -1 on error
  59. */
  60. int os_open(const char *pathname, int flags);
  61. #define OS_O_RDONLY 0
  62. #define OS_O_WRONLY 1
  63. #define OS_O_RDWR 2
  64. #define OS_O_MASK 3 /* Mask for read/write flags */
  65. #define OS_O_CREAT 0100
  66. /**
  67. * Access to the OS close() system call
  68. *
  69. * \param fd File descriptor to close
  70. * \return 0 on success, -1 on error
  71. */
  72. int os_close(int fd);
  73. /**
  74. * Access to the OS exit() system call
  75. *
  76. * This exits with the supplied return code, which should be 0 to indicate
  77. * success.
  78. *
  79. * @param exit_code exit code for U-Boot
  80. */
  81. void os_exit(int exit_code) __attribute__((noreturn));
  82. /**
  83. * Put tty into raw mode to mimic serial console better
  84. */
  85. void os_tty_raw(int fd);
  86. /**
  87. * Acquires some memory from the underlying os.
  88. *
  89. * \param length Number of bytes to be allocated
  90. * \return Pointer to length bytes or NULL on error
  91. */
  92. void *os_malloc(size_t length);
  93. /**
  94. * Access to the usleep function of the os
  95. *
  96. * \param usec Time to sleep in micro seconds
  97. */
  98. void os_usleep(unsigned long usec);
  99. /**
  100. * Gets a monotonic increasing number of nano seconds from the OS
  101. *
  102. * \return A monotonic increasing time scaled in nano seconds
  103. */
  104. u64 os_get_nsec(void);
  105. /**
  106. * Parse arguments and update sandbox state.
  107. *
  108. * @param state Sandbox state to update
  109. * @param argc Argument count
  110. * @param argv Argument vector
  111. * @return 0 if ok, and program should continue;
  112. * 1 if ok, but program should stop;
  113. * -1 on error: program should terminate.
  114. */
  115. int os_parse_args(struct sandbox_state *state, int argc, char *argv[]);
  116. /*
  117. * Types of directory entry that we support. See also os_dirent_typename in
  118. * the C file.
  119. */
  120. enum os_dirent_t {
  121. OS_FILET_REG, /* Regular file */
  122. OS_FILET_LNK, /* Symbolic link */
  123. OS_FILET_DIR, /* Directory */
  124. OS_FILET_UNKNOWN, /* Something else */
  125. OS_FILET_COUNT,
  126. };
  127. /** A directory entry node, containing information about a single dirent */
  128. struct os_dirent_node {
  129. struct os_dirent_node *next; /* Pointer to next node, or NULL */
  130. ulong size; /* Size of file in bytes */
  131. enum os_dirent_t type; /* Type of entry */
  132. char name[0]; /* Name of entry */
  133. };
  134. /**
  135. * Get a directionry listing
  136. *
  137. * This allocates and returns a linked list containing the directory listing.
  138. *
  139. * @param dirname Directory to examine
  140. * @param headp Returns pointer to head of linked list, or NULL if none
  141. * @return 0 if ok, -ve on error
  142. */
  143. int os_dirent_ls(const char *dirname, struct os_dirent_node **headp);
  144. /**
  145. * Get the name of a directory entry type
  146. *
  147. * @param type Type to cehck
  148. * @return string containing the name of that type, or "???" if none/invalid
  149. */
  150. const char *os_dirent_get_typename(enum os_dirent_t type);
  151. /**
  152. * Get the size of a file
  153. *
  154. * @param fname Filename to check
  155. * @return size of file, or -1 if an error ocurred
  156. */
  157. ssize_t os_get_filesize(const char *fname);
  158. #endif