os.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. #include <linux/types.h>
  13. struct sandbox_state;
  14. /**
  15. * Access to the OS read() system call
  16. *
  17. * \param fd File descriptor as returned by os_open()
  18. * \param buf Buffer to place data
  19. * \param count Number of bytes to read
  20. * \return number of bytes read, or -1 on error
  21. */
  22. ssize_t os_read(int fd, void *buf, size_t count);
  23. /**
  24. * Access to the OS read() system call with non-blocking access
  25. *
  26. * \param fd File descriptor as returned by os_open()
  27. * \param buf Buffer to place data
  28. * \param count Number of bytes to read
  29. * \return number of bytes read, or -1 on error
  30. */
  31. ssize_t os_read_no_block(int fd, void *buf, size_t count);
  32. /**
  33. * Access to the OS write() system call
  34. *
  35. * \param fd File descriptor as returned by os_open()
  36. * \param buf Buffer containing data to write
  37. * \param count Number of bytes to write
  38. * \return number of bytes written, or -1 on error
  39. */
  40. ssize_t os_write(int fd, const void *buf, size_t count);
  41. /**
  42. * Access to the OS lseek() system call
  43. *
  44. * \param fd File descriptor as returned by os_open()
  45. * \param offset File offset (based on whence)
  46. * \param whence Position offset is relative to (see below)
  47. * \return new file offset
  48. */
  49. off_t os_lseek(int fd, off_t offset, int whence);
  50. /* Defines for "whence" in os_lseek() */
  51. #define OS_SEEK_SET 0
  52. #define OS_SEEK_CUR 1
  53. #define OS_SEEK_END 2
  54. /**
  55. * Access to the OS open() system call
  56. *
  57. * \param pathname Pathname of file to open
  58. * \param flags Flags, like O_RDONLY, O_RDWR
  59. * \return file descriptor, or -1 on error
  60. */
  61. int os_open(const char *pathname, int flags);
  62. #define OS_O_RDONLY 0
  63. #define OS_O_WRONLY 1
  64. #define OS_O_RDWR 2
  65. #define OS_O_MASK 3 /* Mask for read/write flags */
  66. #define OS_O_CREAT 0100
  67. /**
  68. * Access to the OS close() system call
  69. *
  70. * \param fd File descriptor to close
  71. * \return 0 on success, -1 on error
  72. */
  73. int os_close(int fd);
  74. /**
  75. * Access to the OS exit() system call
  76. *
  77. * This exits with the supplied return code, which should be 0 to indicate
  78. * success.
  79. *
  80. * @param exit_code exit code for U-Boot
  81. */
  82. void os_exit(int exit_code) __attribute__((noreturn));
  83. /**
  84. * Put tty into raw mode to mimic serial console better
  85. */
  86. void os_tty_raw(int fd);
  87. /**
  88. * Acquires some memory from the underlying os.
  89. *
  90. * \param length Number of bytes to be allocated
  91. * \return Pointer to length bytes or NULL on error
  92. */
  93. void *os_malloc(size_t length);
  94. /**
  95. * Free memory previous allocated with os_malloc()/os_realloc()
  96. *
  97. * This returns the memory to the OS.
  98. *
  99. * \param ptr Pointer to memory block to free
  100. */
  101. void *os_free(void *ptr);
  102. /**
  103. * Reallocate previously-allocated memory to increase/decrease space
  104. *
  105. * This works in a similar way to the C library realloc() function. If
  106. * length is 0, then ptr is freed. Otherwise the space used by ptr is
  107. * expanded or reduced depending on whether length is larger or smaller
  108. * than before.
  109. *
  110. * If ptr is NULL, then this is similar to calling os_malloc().
  111. *
  112. * This function may need to move the memory block to make room for any
  113. * extra space, in which case the new pointer is returned.
  114. *
  115. * \param ptr Pointer to memory block to reallocate
  116. * \param length New length for memory block
  117. * \return pointer to new memory block, or NULL on failure or if length
  118. * is 0.
  119. */
  120. void *os_realloc(void *ptr, size_t length);
  121. /**
  122. * Access to the usleep function of the os
  123. *
  124. * \param usec Time to sleep in micro seconds
  125. */
  126. void os_usleep(unsigned long usec);
  127. /**
  128. * Gets a monotonic increasing number of nano seconds from the OS
  129. *
  130. * \return A monotonic increasing time scaled in nano seconds
  131. */
  132. uint64_t os_get_nsec(void);
  133. /**
  134. * Parse arguments and update sandbox state.
  135. *
  136. * @param state Sandbox state to update
  137. * @param argc Argument count
  138. * @param argv Argument vector
  139. * @return 0 if ok, and program should continue;
  140. * 1 if ok, but program should stop;
  141. * -1 on error: program should terminate.
  142. */
  143. int os_parse_args(struct sandbox_state *state, int argc, char *argv[]);
  144. /*
  145. * Types of directory entry that we support. See also os_dirent_typename in
  146. * the C file.
  147. */
  148. enum os_dirent_t {
  149. OS_FILET_REG, /* Regular file */
  150. OS_FILET_LNK, /* Symbolic link */
  151. OS_FILET_DIR, /* Directory */
  152. OS_FILET_UNKNOWN, /* Something else */
  153. OS_FILET_COUNT,
  154. };
  155. /** A directory entry node, containing information about a single dirent */
  156. struct os_dirent_node {
  157. struct os_dirent_node *next; /* Pointer to next node, or NULL */
  158. ulong size; /* Size of file in bytes */
  159. enum os_dirent_t type; /* Type of entry */
  160. char name[0]; /* Name of entry */
  161. };
  162. /**
  163. * Get a directionry listing
  164. *
  165. * This allocates and returns a linked list containing the directory listing.
  166. *
  167. * @param dirname Directory to examine
  168. * @param headp Returns pointer to head of linked list, or NULL if none
  169. * @return 0 if ok, -ve on error
  170. */
  171. int os_dirent_ls(const char *dirname, struct os_dirent_node **headp);
  172. /**
  173. * Get the name of a directory entry type
  174. *
  175. * @param type Type to cehck
  176. * @return string containing the name of that type, or "???" if none/invalid
  177. */
  178. const char *os_dirent_get_typename(enum os_dirent_t type);
  179. /**
  180. * Get the size of a file
  181. *
  182. * @param fname Filename to check
  183. * @return size of file, or -1 if an error ocurred
  184. */
  185. ssize_t os_get_filesize(const char *fname);
  186. /**
  187. * Write a character to the controlling OS terminal
  188. *
  189. * This bypasses the U-Boot console support and writes directly to the OS
  190. * stdout file descriptor.
  191. *
  192. * @param ch Character to write
  193. */
  194. void os_putc(int ch);
  195. /**
  196. * Write a string to the controlling OS terminal
  197. *
  198. * This bypasses the U-Boot console support and writes directly to the OS
  199. * stdout file descriptor.
  200. *
  201. * @param str String to write (note that \n is not appended)
  202. */
  203. void os_puts(const char *str);
  204. /**
  205. * Write the sandbox RAM buffer to a existing file
  206. *
  207. * @param fname Filename to write memory to (simple binary format)
  208. * @return 0 if OK, -ve on error
  209. */
  210. int os_write_ram_buf(const char *fname);
  211. /**
  212. * Read the sandbox RAM buffer from an existing file
  213. *
  214. * @param fname Filename containing memory (simple binary format)
  215. * @return 0 if OK, -ve on error
  216. */
  217. int os_read_ram_buf(const char *fname);
  218. #endif