os.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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 unlink() system call
  76. *
  77. * \param pathname Path of file to delete
  78. * \return 0 for success, other for error
  79. */
  80. int os_unlink(const char *pathname);
  81. /**
  82. * Access to the OS exit() system call
  83. *
  84. * This exits with the supplied return code, which should be 0 to indicate
  85. * success.
  86. *
  87. * @param exit_code exit code for U-Boot
  88. */
  89. void os_exit(int exit_code) __attribute__((noreturn));
  90. /**
  91. * Put tty into raw mode to mimic serial console better
  92. *
  93. * @param fd File descriptor of stdin (normally 0)
  94. * @param allow_sigs Allow Ctrl-C, Ctrl-Z to generate signals rather than
  95. * be handled by U-Boot
  96. */
  97. void os_tty_raw(int fd, bool allow_sigs);
  98. /**
  99. * Acquires some memory from the underlying os.
  100. *
  101. * \param length Number of bytes to be allocated
  102. * \return Pointer to length bytes or NULL on error
  103. */
  104. void *os_malloc(size_t length);
  105. /**
  106. * Free memory previous allocated with os_malloc()/os_realloc()
  107. *
  108. * This returns the memory to the OS.
  109. *
  110. * \param ptr Pointer to memory block to free
  111. */
  112. void os_free(void *ptr);
  113. /**
  114. * Reallocate previously-allocated memory to increase/decrease space
  115. *
  116. * This works in a similar way to the C library realloc() function. If
  117. * length is 0, then ptr is freed. Otherwise the space used by ptr is
  118. * expanded or reduced depending on whether length is larger or smaller
  119. * than before.
  120. *
  121. * If ptr is NULL, then this is similar to calling os_malloc().
  122. *
  123. * This function may need to move the memory block to make room for any
  124. * extra space, in which case the new pointer is returned.
  125. *
  126. * \param ptr Pointer to memory block to reallocate
  127. * \param length New length for memory block
  128. * \return pointer to new memory block, or NULL on failure or if length
  129. * is 0.
  130. */
  131. void *os_realloc(void *ptr, size_t length);
  132. /**
  133. * Access to the usleep function of the os
  134. *
  135. * \param usec Time to sleep in micro seconds
  136. */
  137. void os_usleep(unsigned long usec);
  138. /**
  139. * Gets a monotonic increasing number of nano seconds from the OS
  140. *
  141. * \return A monotonic increasing time scaled in nano seconds
  142. */
  143. uint64_t os_get_nsec(void);
  144. /**
  145. * Parse arguments and update sandbox state.
  146. *
  147. * @param state Sandbox state to update
  148. * @param argc Argument count
  149. * @param argv Argument vector
  150. * @return 0 if ok, and program should continue;
  151. * 1 if ok, but program should stop;
  152. * -1 on error: program should terminate.
  153. */
  154. int os_parse_args(struct sandbox_state *state, int argc, char *argv[]);
  155. /*
  156. * Types of directory entry that we support. See also os_dirent_typename in
  157. * the C file.
  158. */
  159. enum os_dirent_t {
  160. OS_FILET_REG, /* Regular file */
  161. OS_FILET_LNK, /* Symbolic link */
  162. OS_FILET_DIR, /* Directory */
  163. OS_FILET_UNKNOWN, /* Something else */
  164. OS_FILET_COUNT,
  165. };
  166. /** A directory entry node, containing information about a single dirent */
  167. struct os_dirent_node {
  168. struct os_dirent_node *next; /* Pointer to next node, or NULL */
  169. ulong size; /* Size of file in bytes */
  170. enum os_dirent_t type; /* Type of entry */
  171. char name[0]; /* Name of entry */
  172. };
  173. /**
  174. * Get a directionry listing
  175. *
  176. * This allocates and returns a linked list containing the directory listing.
  177. *
  178. * @param dirname Directory to examine
  179. * @param headp Returns pointer to head of linked list, or NULL if none
  180. * @return 0 if ok, -ve on error
  181. */
  182. int os_dirent_ls(const char *dirname, struct os_dirent_node **headp);
  183. /**
  184. * Get the name of a directory entry type
  185. *
  186. * @param type Type to cehck
  187. * @return string containing the name of that type, or "???" if none/invalid
  188. */
  189. const char *os_dirent_get_typename(enum os_dirent_t type);
  190. /**
  191. * Get the size of a file
  192. *
  193. * @param fname Filename to check
  194. * @param size size of file is returned if no error
  195. * @return 0 on success or -1 if an error ocurred
  196. */
  197. int os_get_filesize(const char *fname, loff_t *size);
  198. /**
  199. * Write a character to the controlling OS terminal
  200. *
  201. * This bypasses the U-Boot console support and writes directly to the OS
  202. * stdout file descriptor.
  203. *
  204. * @param ch Character to write
  205. */
  206. void os_putc(int ch);
  207. /**
  208. * Write a string to the controlling OS terminal
  209. *
  210. * This bypasses the U-Boot console support and writes directly to the OS
  211. * stdout file descriptor.
  212. *
  213. * @param str String to write (note that \n is not appended)
  214. */
  215. void os_puts(const char *str);
  216. /**
  217. * Write the sandbox RAM buffer to a existing file
  218. *
  219. * @param fname Filename to write memory to (simple binary format)
  220. * @return 0 if OK, -ve on error
  221. */
  222. int os_write_ram_buf(const char *fname);
  223. /**
  224. * Read the sandbox RAM buffer from an existing file
  225. *
  226. * @param fname Filename containing memory (simple binary format)
  227. * @return 0 if OK, -ve on error
  228. */
  229. int os_read_ram_buf(const char *fname);
  230. /**
  231. * Jump to a new executable image
  232. *
  233. * This uses exec() to run a new executable image, after putting it in a
  234. * temporary file. The same arguments and environment are passed to this
  235. * new image, with the addition of:
  236. *
  237. * -j <filename> Specifies the filename the image was written to. The
  238. * calling image may want to delete this at some point.
  239. * -m <filename> Specifies the file containing the sandbox memory
  240. * (ram_buf) from this image, so that the new image can
  241. * have access to this. It also means that the original
  242. * memory filename passed to U-Boot will be left intact.
  243. *
  244. * @param dest Buffer containing executable image
  245. * @param size Size of buffer
  246. */
  247. int os_jump_to_image(const void *dest, int size);
  248. #endif