os.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* SPDX-License-Identifier: GPL-2.0+ */
  2. /*
  3. * Operating System Interface
  4. *
  5. * This provides access to useful OS routines for the sandbox architecture.
  6. * They are kept in a separate file so we can include system headers.
  7. *
  8. * Copyright (c) 2011 The Chromium OS Authors.
  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. * Restore the tty to its original mode
  101. *
  102. * Call this to restore the original terminal mode, after it has been changed
  103. * by os_tty_raw(). This is an internal function.
  104. */
  105. void os_fd_restore(void);
  106. /**
  107. * Acquires some memory from the underlying os.
  108. *
  109. * \param length Number of bytes to be allocated
  110. * \return Pointer to length bytes or NULL on error
  111. */
  112. void *os_malloc(size_t length);
  113. /**
  114. * Free memory previous allocated with os_malloc()/os_realloc()
  115. *
  116. * This returns the memory to the OS.
  117. *
  118. * \param ptr Pointer to memory block to free
  119. */
  120. void os_free(void *ptr);
  121. /**
  122. * Reallocate previously-allocated memory to increase/decrease space
  123. *
  124. * This works in a similar way to the C library realloc() function. If
  125. * length is 0, then ptr is freed. Otherwise the space used by ptr is
  126. * expanded or reduced depending on whether length is larger or smaller
  127. * than before.
  128. *
  129. * If ptr is NULL, then this is similar to calling os_malloc().
  130. *
  131. * This function may need to move the memory block to make room for any
  132. * extra space, in which case the new pointer is returned.
  133. *
  134. * \param ptr Pointer to memory block to reallocate
  135. * \param length New length for memory block
  136. * \return pointer to new memory block, or NULL on failure or if length
  137. * is 0.
  138. */
  139. void *os_realloc(void *ptr, size_t length);
  140. /**
  141. * Access to the usleep function of the os
  142. *
  143. * \param usec Time to sleep in micro seconds
  144. */
  145. void os_usleep(unsigned long usec);
  146. /**
  147. * Gets a monotonic increasing number of nano seconds from the OS
  148. *
  149. * \return A monotonic increasing time scaled in nano seconds
  150. */
  151. uint64_t os_get_nsec(void);
  152. /**
  153. * Parse arguments and update sandbox state.
  154. *
  155. * @param state Sandbox state to update
  156. * @param argc Argument count
  157. * @param argv Argument vector
  158. * @return 0 if ok, and program should continue;
  159. * 1 if ok, but program should stop;
  160. * -1 on error: program should terminate.
  161. */
  162. int os_parse_args(struct sandbox_state *state, int argc, char *argv[]);
  163. /*
  164. * Types of directory entry that we support. See also os_dirent_typename in
  165. * the C file.
  166. */
  167. enum os_dirent_t {
  168. OS_FILET_REG, /* Regular file */
  169. OS_FILET_LNK, /* Symbolic link */
  170. OS_FILET_DIR, /* Directory */
  171. OS_FILET_UNKNOWN, /* Something else */
  172. OS_FILET_COUNT,
  173. };
  174. /** A directory entry node, containing information about a single dirent */
  175. struct os_dirent_node {
  176. struct os_dirent_node *next; /* Pointer to next node, or NULL */
  177. ulong size; /* Size of file in bytes */
  178. enum os_dirent_t type; /* Type of entry */
  179. char name[0]; /* Name of entry */
  180. };
  181. /**
  182. * Get a directionry listing
  183. *
  184. * This allocates and returns a linked list containing the directory listing.
  185. *
  186. * @param dirname Directory to examine
  187. * @param headp Returns pointer to head of linked list, or NULL if none
  188. * @return 0 if ok, -ve on error
  189. */
  190. int os_dirent_ls(const char *dirname, struct os_dirent_node **headp);
  191. /**
  192. * Free directory list
  193. *
  194. * This frees a linked list containing a directory listing.
  195. *
  196. * @param node Pointer to head of linked list
  197. */
  198. void os_dirent_free(struct os_dirent_node *node);
  199. /**
  200. * Get the name of a directory entry type
  201. *
  202. * @param type Type to check
  203. * @return string containing the name of that type, or "???" if none/invalid
  204. */
  205. const char *os_dirent_get_typename(enum os_dirent_t type);
  206. /**
  207. * Get the size of a file
  208. *
  209. * @param fname Filename to check
  210. * @param size size of file is returned if no error
  211. * @return 0 on success or -1 if an error ocurred
  212. */
  213. int os_get_filesize(const char *fname, loff_t *size);
  214. /**
  215. * Write a character to the controlling OS terminal
  216. *
  217. * This bypasses the U-Boot console support and writes directly to the OS
  218. * stdout file descriptor.
  219. *
  220. * @param ch Character to write
  221. */
  222. void os_putc(int ch);
  223. /**
  224. * Write a string to the controlling OS terminal
  225. *
  226. * This bypasses the U-Boot console support and writes directly to the OS
  227. * stdout file descriptor.
  228. *
  229. * @param str String to write (note that \n is not appended)
  230. */
  231. void os_puts(const char *str);
  232. /**
  233. * Write the sandbox RAM buffer to a existing file
  234. *
  235. * @param fname Filename to write memory to (simple binary format)
  236. * @return 0 if OK, -ve on error
  237. */
  238. int os_write_ram_buf(const char *fname);
  239. /**
  240. * Read the sandbox RAM buffer from an existing file
  241. *
  242. * @param fname Filename containing memory (simple binary format)
  243. * @return 0 if OK, -ve on error
  244. */
  245. int os_read_ram_buf(const char *fname);
  246. /**
  247. * Jump to a new executable image
  248. *
  249. * This uses exec() to run a new executable image, after putting it in a
  250. * temporary file. The same arguments and environment are passed to this
  251. * new image, with the addition of:
  252. *
  253. * -j <filename> Specifies the filename the image was written to. The
  254. * calling image may want to delete this at some point.
  255. * -m <filename> Specifies the file containing the sandbox memory
  256. * (ram_buf) from this image, so that the new image can
  257. * have access to this. It also means that the original
  258. * memory filename passed to U-Boot will be left intact.
  259. *
  260. * @param dest Buffer containing executable image
  261. * @param size Size of buffer
  262. */
  263. int os_jump_to_image(const void *dest, int size);
  264. /**
  265. * os_find_u_boot() - Determine the path to U-Boot proper
  266. *
  267. * This function is intended to be called from within sandbox SPL. It uses
  268. * a few heuristics to find U-Boot proper. Normally it is either in the same
  269. * directory, or the directory above (since u-boot-spl is normally in an
  270. * spl/ subdirectory when built).
  271. *
  272. * @fname: Place to put full path to U-Boot
  273. * @maxlen: Maximum size of @fname
  274. * @return 0 if OK, -NOSPC if the filename is too large, -ENOENT if not found
  275. */
  276. int os_find_u_boot(char *fname, int maxlen);
  277. /**
  278. * os_spl_to_uboot() - Run U-Boot proper
  279. *
  280. * When called from SPL, this runs U-Boot proper. The filename is obtained by
  281. * calling os_find_u_boot().
  282. *
  283. * @fname: Full pathname to U-Boot executable
  284. * @return 0 if OK, -ve on error
  285. */
  286. int os_spl_to_uboot(const char *fname);
  287. /**
  288. * Read the current system time
  289. *
  290. * This reads the current Local Time and places it into the provided
  291. * structure.
  292. *
  293. * @param rt Place to put system time
  294. */
  295. void os_localtime(struct rtc_time *rt);
  296. #endif