os.h 7.7 KB

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