os.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. * @fd: File descriptor as returned by os_open()
  19. * @buf: Buffer to place data
  20. * @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 write() system call
  26. *
  27. * @fd: File descriptor as returned by os_open()
  28. * @buf: Buffer containing data to write
  29. * @count: Number of bytes to write
  30. * Return: number of bytes written, or -1 on error
  31. */
  32. ssize_t os_write(int fd, const void *buf, size_t count);
  33. /**
  34. * Access to the OS lseek() system call
  35. *
  36. * @fd: File descriptor as returned by os_open()
  37. * @offset: File offset (based on whence)
  38. * @whence: Position offset is relative to (see below)
  39. * Return: new file offset
  40. */
  41. off_t os_lseek(int fd, off_t offset, int whence);
  42. /* Defines for "whence" in os_lseek() */
  43. #define OS_SEEK_SET 0
  44. #define OS_SEEK_CUR 1
  45. #define OS_SEEK_END 2
  46. /**
  47. * Access to the OS open() system call
  48. *
  49. * @pathname: Pathname of file to open
  50. * @flags: Flags, like OS_O_RDONLY, OS_O_RDWR
  51. * Return: file descriptor, or -1 on error
  52. */
  53. int os_open(const char *pathname, int flags);
  54. #define OS_O_RDONLY 0
  55. #define OS_O_WRONLY 1
  56. #define OS_O_RDWR 2
  57. #define OS_O_MASK 3 /* Mask for read/write flags */
  58. #define OS_O_CREAT 0100
  59. #define OS_O_TRUNC 01000
  60. /**
  61. * os_close() - access to the OS close() system call
  62. *
  63. * @fd: File descriptor to close
  64. * Return: 0 on success, -1 on error
  65. */
  66. int os_close(int fd);
  67. /**
  68. * os_unlink() - access to the OS unlink() system call
  69. *
  70. * @pathname: Path of file to delete
  71. * Return: 0 for success, other for error
  72. */
  73. int os_unlink(const char *pathname);
  74. /**
  75. * os_exit() - 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. * @exit_code: exit code for U-Boot
  81. */
  82. void os_exit(int exit_code) __attribute__((noreturn));
  83. /**
  84. * os_tty_raw() - put tty into raw mode to mimic serial console better
  85. *
  86. * @fd: File descriptor of stdin (normally 0)
  87. * @allow_sigs: Allow Ctrl-C, Ctrl-Z to generate signals rather than
  88. * be handled by U-Boot
  89. */
  90. void os_tty_raw(int fd, bool allow_sigs);
  91. /**
  92. * os_fs_restore() - restore the tty to its original mode
  93. *
  94. * Call this to restore the original terminal mode, after it has been changed
  95. * by os_tty_raw(). This is an internal function.
  96. */
  97. void os_fd_restore(void);
  98. /**
  99. * os_malloc() - aquires some memory from the underlying os.
  100. *
  101. * @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. * os_free() - free memory previous allocated with os_malloc()
  107. *
  108. * This returns the memory to the OS.
  109. *
  110. * @ptr: Pointer to memory block to free
  111. */
  112. void os_free(void *ptr);
  113. /**
  114. * os_usleep() - access to the usleep function of the os
  115. *
  116. * @usec: time to sleep in micro seconds
  117. */
  118. void os_usleep(unsigned long usec);
  119. /**
  120. * Gets a monotonic increasing number of nano seconds from the OS
  121. *
  122. * Return: a monotonic increasing time scaled in nano seconds
  123. */
  124. uint64_t os_get_nsec(void);
  125. /**
  126. * Parse arguments and update sandbox state.
  127. *
  128. * @state: sandbox state to update
  129. * @argc: argument count
  130. * @argv: argument vector
  131. * Return:
  132. * * 0 if ok, and program should continue
  133. * * 1 if ok, but program should stop
  134. * * -1 on error: program should terminate
  135. */
  136. int os_parse_args(struct sandbox_state *state, int argc, char *argv[]);
  137. /*
  138. * enum os_dirent_t - type of directory entry
  139. *
  140. * Types of directory entry that we support. See also os_dirent_typename in
  141. * the C file.
  142. */
  143. enum os_dirent_t {
  144. /**
  145. * @OS_FILET_REG: regular file
  146. */
  147. OS_FILET_REG,
  148. /**
  149. * @OS_FILET_LNK: symbolic link
  150. */
  151. OS_FILET_LNK,
  152. /**
  153. * @OS_FILET_DIR: directory
  154. */
  155. OS_FILET_DIR,
  156. /**
  157. * @OS_FILET_UNKNOWN: something else
  158. */
  159. OS_FILET_UNKNOWN,
  160. /**
  161. * @OS_FILET_COUNT: number of directory entry types
  162. */
  163. OS_FILET_COUNT,
  164. };
  165. /**
  166. * struct os_dirent_node - directory node
  167. *
  168. * A directory entry node, containing information about a single dirent
  169. *
  170. */
  171. struct os_dirent_node {
  172. /**
  173. * @next: pointer to next node, or NULL
  174. */
  175. struct os_dirent_node *next;
  176. /**
  177. * @size: size of file in bytes
  178. */
  179. ulong size;
  180. /**
  181. * @type: type of entry
  182. */
  183. enum os_dirent_t type;
  184. /**
  185. * @name: name of entry
  186. */
  187. char name[0];
  188. };
  189. /**
  190. * os_dirent_ls() - get a directory listing
  191. *
  192. * This allocates and returns a linked list containing the directory listing.
  193. *
  194. * @dirname: directory to examine
  195. * @headp: on return pointer to head of linked list, or NULL if none
  196. * Return: 0 if ok, -ve on error
  197. */
  198. int os_dirent_ls(const char *dirname, struct os_dirent_node **headp);
  199. /**
  200. * os_dirent_free() - free directory list
  201. *
  202. * This frees a linked list containing a directory listing.
  203. *
  204. * @node: pointer to head of linked list
  205. */
  206. void os_dirent_free(struct os_dirent_node *node);
  207. /**
  208. * os_dirent_get_typename() - get the name of a directory entry type
  209. *
  210. * @type: type to check
  211. * Return:
  212. * string containing the name of that type,
  213. * or "???" if none/invalid
  214. */
  215. const char *os_dirent_get_typename(enum os_dirent_t type);
  216. /**
  217. * os_get_filesize() - get the size of a file
  218. *
  219. * @fname: filename to check
  220. * @size: size of file is returned if no error
  221. * Return: 0 on success or -1 if an error ocurred
  222. */
  223. int os_get_filesize(const char *fname, loff_t *size);
  224. /**
  225. * os_putc() - write a character to the controlling OS terminal
  226. *
  227. * This bypasses the U-Boot console support and writes directly to the OS
  228. * stdout file descriptor.
  229. *
  230. * @ch: haracter to write
  231. */
  232. void os_putc(int ch);
  233. /**
  234. * os_puts() - write a string to the controlling OS terminal
  235. *
  236. * This bypasses the U-Boot console support and writes directly to the OS
  237. * stdout file descriptor.
  238. *
  239. * @str: string to write (note that \n is not appended)
  240. */
  241. void os_puts(const char *str);
  242. /**
  243. * os_write_ram_buf() - write the sandbox RAM buffer to a existing file
  244. *
  245. * @fname: filename to write memory to (simple binary format)
  246. * Return: 0 if OK, -ve on error
  247. */
  248. int os_write_ram_buf(const char *fname);
  249. /**
  250. * os_read_ram_buf() - read the sandbox RAM buffer from an existing file
  251. *
  252. * @fname: filename containing memory (simple binary format)
  253. * Return: 0 if OK, -ve on error
  254. */
  255. int os_read_ram_buf(const char *fname);
  256. /**
  257. * os_jump_to_image() - jump to a new executable image
  258. *
  259. * This uses exec() to run a new executable image, after putting it in a
  260. * temporary file. The same arguments and environment are passed to this
  261. * new image, with the addition of:
  262. *
  263. * -j <filename> Specifies the filename the image was written to. The
  264. * calling image may want to delete this at some point.
  265. * -m <filename> Specifies the file containing the sandbox memory
  266. * (ram_buf) from this image, so that the new image can
  267. * have access to this. It also means that the original
  268. * memory filename passed to U-Boot will be left intact.
  269. *
  270. * @dest: buffer containing executable image
  271. * @size: size of buffer
  272. * Return: 0 if OK, -ve on error
  273. */
  274. int os_jump_to_image(const void *dest, int size);
  275. /**
  276. * os_find_u_boot() - determine the path to U-Boot proper
  277. *
  278. * This function is intended to be called from within sandbox SPL. It uses
  279. * a few heuristics to find U-Boot proper. Normally it is either in the same
  280. * directory, or the directory above (since u-boot-spl is normally in an
  281. * spl/ subdirectory when built).
  282. *
  283. * @fname: place to put full path to U-Boot
  284. * @maxlen: maximum size of @fname
  285. * Return: 0 if OK, -NOSPC if the filename is too large, -ENOENT if not found
  286. */
  287. int os_find_u_boot(char *fname, int maxlen);
  288. /**
  289. * os_spl_to_uboot() - Run U-Boot proper
  290. *
  291. * When called from SPL, this runs U-Boot proper. The filename is obtained by
  292. * calling os_find_u_boot().
  293. *
  294. * @fname: full pathname to U-Boot executable
  295. * Return: 0 if OK, -ve on error
  296. */
  297. int os_spl_to_uboot(const char *fname);
  298. /**
  299. * os_localtime() - read the current system time
  300. *
  301. * This reads the current Local Time and places it into the provided
  302. * structure.
  303. *
  304. * @rt: place to put system time
  305. */
  306. void os_localtime(struct rtc_time *rt);
  307. /**
  308. * os_abort() - raise SIGABRT to exit sandbox (e.g. to debugger)
  309. */
  310. void os_abort(void);
  311. /**
  312. * os_mprotect_allow() - Remove write-protection on a region of memory
  313. *
  314. * The start and length will be page-aligned before use.
  315. *
  316. * @start: Region start
  317. * @len: Region length in bytes
  318. * Return: 0 if OK, -1 on error from mprotect()
  319. */
  320. int os_mprotect_allow(void *start, size_t len);
  321. /**
  322. * os_write_file() - write a file to the host filesystem
  323. *
  324. * This can be useful when debugging for writing data out of sandbox for
  325. * inspection by external tools.
  326. *
  327. * @name: File path to write to
  328. * @buf: Data to write
  329. * @size: Size of data to write
  330. * Return: 0 if OK, -ve on error
  331. */
  332. int os_write_file(const char *name, const void *buf, int size);
  333. /**
  334. * os_read_file() - Read a file from the host filesystem
  335. *
  336. * This can be useful when reading test data into sandbox for use by test
  337. * routines. The data is allocated using os_malloc() and should be freed by
  338. * the caller.
  339. *
  340. * @name: File path to read from
  341. * @bufp: Returns buffer containing data read
  342. * @sizep: Returns size of data
  343. * Return: 0 if OK, -ve on error
  344. */
  345. int os_read_file(const char *name, void **bufp, int *sizep);
  346. /*
  347. * os_find_text_base() - Find the text section in this running process
  348. *
  349. * This tries to find the address of the text section in this running process.
  350. * It can be useful to map the address of functions to the address listed in
  351. * the u-boot.map file.
  352. *
  353. * Return: address if found, else NULL
  354. */
  355. void *os_find_text_base(void);
  356. /**
  357. * os_relaunch() - restart the sandbox
  358. *
  359. * This functions is used to implement the cold reboot of the sand box.
  360. * @argv\[0] specifies the binary that is started while the calling process
  361. * stops immediately. If the new binary cannot be started, the process is
  362. * terminated and 1 is set as shell return code.
  363. *
  364. * The PID of the process stays the same. All file descriptors that have not
  365. * been opened with O_CLOEXEC stay open including stdin, stdout, stderr.
  366. *
  367. * @argv: NULL terminated list of command line parameters
  368. */
  369. void os_relaunch(char *argv[]);
  370. #endif