os.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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. * os_printf() - print directly to OS console
  17. *
  18. * @format: format string
  19. */
  20. int os_printf(const char *format, ...);
  21. /**
  22. * Access to the OS read() system call
  23. *
  24. * @fd: File descriptor as returned by os_open()
  25. * @buf: Buffer to place data
  26. * @count: Number of bytes to read
  27. * Return: number of bytes read, or -1 on error
  28. */
  29. ssize_t os_read(int fd, void *buf, size_t count);
  30. /**
  31. * Access to the OS write() system call
  32. *
  33. * @fd: File descriptor as returned by os_open()
  34. * @buf: Buffer containing data to write
  35. * @count: Number of bytes to write
  36. * Return: number of bytes written, or -1 on error
  37. */
  38. ssize_t os_write(int fd, const void *buf, size_t count);
  39. /**
  40. * Access to the OS lseek() system call
  41. *
  42. * @fd: File descriptor as returned by os_open()
  43. * @offset: File offset (based on whence)
  44. * @whence: Position offset is relative to (see below)
  45. * Return: new file offset
  46. */
  47. off_t os_lseek(int fd, off_t offset, int whence);
  48. /* Defines for "whence" in os_lseek() */
  49. #define OS_SEEK_SET 0
  50. #define OS_SEEK_CUR 1
  51. #define OS_SEEK_END 2
  52. /**
  53. * os_filesize() - Calculate the size of a file
  54. *
  55. * @fd: File descriptor as returned by os_open()
  56. * Return: file size or negative error code
  57. */
  58. off_t os_filesize(int fd);
  59. /**
  60. * Access to the OS open() system call
  61. *
  62. * @pathname: Pathname of file to open
  63. * @flags: Flags, like OS_O_RDONLY, OS_O_RDWR
  64. * Return: file descriptor, or -1 on error
  65. */
  66. int os_open(const char *pathname, int flags);
  67. #define OS_O_RDONLY 0
  68. #define OS_O_WRONLY 1
  69. #define OS_O_RDWR 2
  70. #define OS_O_MASK 3 /* Mask for read/write flags */
  71. #define OS_O_CREAT 0100
  72. #define OS_O_TRUNC 01000
  73. /**
  74. * os_close() - access to the OS close() system call
  75. *
  76. * @fd: File descriptor to close
  77. * Return: 0 on success, -1 on error
  78. */
  79. int os_close(int fd);
  80. /**
  81. * os_unlink() - access to the OS unlink() system call
  82. *
  83. * @pathname: Path of file to delete
  84. * Return: 0 for success, other for error
  85. */
  86. int os_unlink(const char *pathname);
  87. /**
  88. * os_exit() - access to the OS exit() system call
  89. *
  90. * This exits with the supplied return code, which should be 0 to indicate
  91. * success.
  92. *
  93. * @exit_code: exit code for U-Boot
  94. */
  95. void os_exit(int exit_code) __attribute__((noreturn));
  96. /**
  97. * os_alarm() - access to the OS alarm() system call
  98. *
  99. * @seconds: number of seconds before the signal is sent
  100. * Returns: number of seconds remaining until any previously scheduled alarm was
  101. * due to be delivered; 0 if there was no previously scheduled alarm
  102. */
  103. unsigned int os_alarm(unsigned int seconds);
  104. /**
  105. * os_set_alarm_handler() - set handler for SIGALRM
  106. *
  107. * @handler: The handler function. Pass NULL for SIG_DFL.
  108. */
  109. void os_set_alarm_handler(void (*handler)(int));
  110. /**
  111. * os_raise_sigalrm() - do raise(SIGALRM)
  112. */
  113. void os_raise_sigalrm(void);
  114. /**
  115. * os_tty_raw() - put tty into raw mode to mimic serial console better
  116. *
  117. * @fd: File descriptor of stdin (normally 0)
  118. * @allow_sigs: Allow Ctrl-C, Ctrl-Z to generate signals rather than
  119. * be handled by U-Boot
  120. */
  121. void os_tty_raw(int fd, bool allow_sigs);
  122. /**
  123. * os_fs_restore() - restore the tty to its original mode
  124. *
  125. * Call this to restore the original terminal mode, after it has been changed
  126. * by os_tty_raw(). This is an internal function.
  127. */
  128. void os_fd_restore(void);
  129. /**
  130. * os_malloc() - aquires some memory from the underlying os.
  131. *
  132. * @length: Number of bytes to be allocated
  133. * Return: Pointer to length bytes or NULL if @length is 0 or on error
  134. */
  135. void *os_malloc(size_t length);
  136. /**
  137. * os_free() - free memory previous allocated with os_malloc()
  138. *
  139. * This returns the memory to the OS.
  140. *
  141. * @ptr: Pointer to memory block to free. If this is NULL then this
  142. * function does nothing
  143. */
  144. void os_free(void *ptr);
  145. /**
  146. * os_realloc() - reallocate memory
  147. *
  148. * This follows the semantics of realloc(), so can perform an os_malloc() or
  149. * os_free() depending on @ptr and @length.
  150. *
  151. * @ptr: pointer to previously allocated memory of NULL
  152. * @length: number of bytes to allocate
  153. * Return: pointer to reallocated memory or NULL if @length is 0
  154. */
  155. void *os_realloc(void *ptr, size_t length);
  156. /**
  157. * os_usleep() - access to the usleep function of the os
  158. *
  159. * @usec: time to sleep in micro seconds
  160. */
  161. void os_usleep(unsigned long usec);
  162. /**
  163. * Gets a monotonic increasing number of nano seconds from the OS
  164. *
  165. * Return: a monotonic increasing time scaled in nano seconds
  166. */
  167. uint64_t os_get_nsec(void);
  168. /**
  169. * Parse arguments and update sandbox state.
  170. *
  171. * @state: sandbox state to update
  172. * @argc: argument count
  173. * @argv: argument vector
  174. * Return:
  175. * * 0 if ok, and program should continue
  176. * * 1 if ok, but program should stop
  177. * * -1 on error: program should terminate
  178. */
  179. int os_parse_args(struct sandbox_state *state, int argc, char *argv[]);
  180. /*
  181. * enum os_dirent_t - type of directory entry
  182. *
  183. * Types of directory entry that we support. See also os_dirent_typename in
  184. * the C file.
  185. */
  186. enum os_dirent_t {
  187. /**
  188. * @OS_FILET_REG: regular file
  189. */
  190. OS_FILET_REG,
  191. /**
  192. * @OS_FILET_LNK: symbolic link
  193. */
  194. OS_FILET_LNK,
  195. /**
  196. * @OS_FILET_DIR: directory
  197. */
  198. OS_FILET_DIR,
  199. /**
  200. * @OS_FILET_UNKNOWN: something else
  201. */
  202. OS_FILET_UNKNOWN,
  203. /**
  204. * @OS_FILET_COUNT: number of directory entry types
  205. */
  206. OS_FILET_COUNT,
  207. };
  208. /**
  209. * struct os_dirent_node - directory node
  210. *
  211. * A directory entry node, containing information about a single dirent
  212. *
  213. */
  214. struct os_dirent_node {
  215. /**
  216. * @next: pointer to next node, or NULL
  217. */
  218. struct os_dirent_node *next;
  219. /**
  220. * @size: size of file in bytes
  221. */
  222. ulong size;
  223. /**
  224. * @type: type of entry
  225. */
  226. enum os_dirent_t type;
  227. /**
  228. * @name: name of entry
  229. */
  230. char name[0];
  231. };
  232. /**
  233. * os_dirent_ls() - get a directory listing
  234. *
  235. * This allocates and returns a linked list containing the directory listing.
  236. *
  237. * @dirname: directory to examine
  238. * @headp: on return pointer to head of linked list, or NULL if none
  239. * Return: 0 if ok, -ve on error
  240. */
  241. int os_dirent_ls(const char *dirname, struct os_dirent_node **headp);
  242. /**
  243. * os_dirent_free() - free directory list
  244. *
  245. * This frees a linked list containing a directory listing.
  246. *
  247. * @node: pointer to head of linked list
  248. */
  249. void os_dirent_free(struct os_dirent_node *node);
  250. /**
  251. * os_dirent_get_typename() - get the name of a directory entry type
  252. *
  253. * @type: type to check
  254. * Return:
  255. * string containing the name of that type,
  256. * or "???" if none/invalid
  257. */
  258. const char *os_dirent_get_typename(enum os_dirent_t type);
  259. /**
  260. * os_get_filesize() - get the size of a file
  261. *
  262. * @fname: filename to check
  263. * @size: size of file is returned if no error
  264. * Return: 0 on success or -1 if an error ocurred
  265. */
  266. int os_get_filesize(const char *fname, long long *size);
  267. /**
  268. * os_putc() - write a character to the controlling OS terminal
  269. *
  270. * This bypasses the U-Boot console support and writes directly to the OS
  271. * stdout file descriptor.
  272. *
  273. * @ch: haracter to write
  274. */
  275. void os_putc(int ch);
  276. /**
  277. * os_puts() - write a string to the controlling OS terminal
  278. *
  279. * This bypasses the U-Boot console support and writes directly to the OS
  280. * stdout file descriptor.
  281. *
  282. * @str: string to write (note that \n is not appended)
  283. */
  284. void os_puts(const char *str);
  285. /**
  286. * os_flush() - flush controlling OS terminal
  287. *
  288. * This bypasses the U-Boot console support and flushes directly the OS
  289. * stdout file descriptor.
  290. */
  291. void os_flush(void);
  292. /**
  293. * os_write_ram_buf() - write the sandbox RAM buffer to a existing file
  294. *
  295. * @fname: filename to write memory to (simple binary format)
  296. * Return: 0 if OK, -ve on error
  297. */
  298. int os_write_ram_buf(const char *fname);
  299. /**
  300. * os_read_ram_buf() - read the sandbox RAM buffer from an existing file
  301. *
  302. * @fname: filename containing memory (simple binary format)
  303. * Return: 0 if OK, -ve on error
  304. */
  305. int os_read_ram_buf(const char *fname);
  306. /**
  307. * os_jump_to_image() - jump to a new executable image
  308. *
  309. * This uses exec() to run a new executable image, after putting it in a
  310. * temporary file. The same arguments and environment are passed to this
  311. * new image, with the addition of:
  312. *
  313. * -j <filename> Specifies the filename the image was written to. The
  314. * calling image may want to delete this at some point.
  315. * -m <filename> Specifies the file containing the sandbox memory
  316. * (ram_buf) from this image, so that the new image can
  317. * have access to this. It also means that the original
  318. * memory filename passed to U-Boot will be left intact.
  319. *
  320. * @dest: buffer containing executable image
  321. * @size: size of buffer
  322. * Return: 0 if OK, -ve on error
  323. */
  324. int os_jump_to_image(const void *dest, int size);
  325. /**
  326. * os_find_u_boot() - determine the path to U-Boot proper
  327. *
  328. * This function is intended to be called from within sandbox SPL. It uses
  329. * a few heuristics to find U-Boot proper. Normally it is either in the same
  330. * directory, or the directory above (since u-boot-spl is normally in an
  331. * spl/ subdirectory when built).
  332. *
  333. * @fname: place to put full path to U-Boot
  334. * @maxlen: maximum size of @fname
  335. * @use_img: select the 'u-boot.img' file instead of the 'u-boot' ELF file
  336. * @cur_prefix: prefix of current executable, e.g. "spl" or "tpl"
  337. * @next_prefix: prefix of executable to find, e.g. "spl" or ""
  338. * Return: 0 if OK, -NOSPC if the filename is too large, -ENOENT if not found
  339. */
  340. int os_find_u_boot(char *fname, int maxlen, bool use_img,
  341. const char *cur_prefix, const char *next_prefix);
  342. /**
  343. * os_spl_to_uboot() - Run U-Boot proper
  344. *
  345. * When called from SPL, this runs U-Boot proper. The filename is obtained by
  346. * calling os_find_u_boot().
  347. *
  348. * @fname: full pathname to U-Boot executable
  349. * Return: 0 if OK, -ve on error
  350. */
  351. int os_spl_to_uboot(const char *fname);
  352. /**
  353. * os_localtime() - read the current system time
  354. *
  355. * This reads the current Local Time and places it into the provided
  356. * structure.
  357. *
  358. * @rt: place to put system time
  359. */
  360. void os_localtime(struct rtc_time *rt);
  361. /**
  362. * os_abort() - raise SIGABRT to exit sandbox (e.g. to debugger)
  363. */
  364. void os_abort(void) __attribute__((noreturn));
  365. /**
  366. * os_mprotect_allow() - Remove write-protection on a region of memory
  367. *
  368. * The start and length will be page-aligned before use.
  369. *
  370. * @start: Region start
  371. * @len: Region length in bytes
  372. * Return: 0 if OK, -1 on error from mprotect()
  373. */
  374. int os_mprotect_allow(void *start, size_t len);
  375. /**
  376. * os_write_file() - write a file to the host filesystem
  377. *
  378. * This can be useful when debugging for writing data out of sandbox for
  379. * inspection by external tools.
  380. *
  381. * @name: File path to write to
  382. * @buf: Data to write
  383. * @size: Size of data to write
  384. * Return: 0 if OK, -ve on error
  385. */
  386. int os_write_file(const char *name, const void *buf, int size);
  387. /**
  388. * os_read_file() - Read a file from the host filesystem
  389. *
  390. * This can be useful when reading test data into sandbox for use by test
  391. * routines. The data is allocated using os_malloc() and should be freed by
  392. * the caller.
  393. *
  394. * @name: File path to read from
  395. * @bufp: Returns buffer containing data read
  396. * @sizep: Returns size of data
  397. * Return: 0 if OK, -ve on error
  398. */
  399. int os_read_file(const char *name, void **bufp, int *sizep);
  400. /**
  401. * os_map_file() - Map a file from the host filesystem into memory
  402. *
  403. * This can be useful when to provide a backing store for an emulated device
  404. *
  405. * @pathname: File pathname to map
  406. * @os_flags: Flags, like OS_O_RDONLY, OS_O_RDWR
  407. * @bufp: Returns buffer containing the file
  408. * @sizep: Returns size of data
  409. * Return: 0 if OK, -ve on error
  410. */
  411. int os_map_file(const char *pathname, int os_flags, void **bufp, int *sizep);
  412. /**
  413. * os_unmap() - Unmap a file previously mapped
  414. *
  415. * @buf: Mapped address
  416. * @size: Size in bytes
  417. * Return: 0 if OK, -ve on error
  418. */
  419. int os_unmap(void *buf, int size);
  420. /*
  421. * os_find_text_base() - Find the text section in this running process
  422. *
  423. * This tries to find the address of the text section in this running process.
  424. * It can be useful to map the address of functions to the address listed in
  425. * the u-boot.map file.
  426. *
  427. * Return: address if found, else NULL
  428. */
  429. void *os_find_text_base(void);
  430. /**
  431. * os_relaunch() - restart the sandbox
  432. *
  433. * This functions is used to implement the cold reboot of the sand box.
  434. * @argv\[0] specifies the binary that is started while the calling process
  435. * stops immediately. If the new binary cannot be started, the process is
  436. * terminated and 1 is set as shell return code.
  437. *
  438. * The PID of the process stays the same. All file descriptors that have not
  439. * been opened with O_CLOEXEC stay open including stdin, stdout, stderr.
  440. *
  441. * @argv: NULL terminated list of command line parameters
  442. */
  443. void os_relaunch(char *argv[]);
  444. /**
  445. * os_setup_signal_handlers() - setup signal handlers
  446. *
  447. * Install signal handlers for SIGBUS and SIGSEGV.
  448. *
  449. * Return: 0 for success
  450. */
  451. int os_setup_signal_handlers(void);
  452. /**
  453. * os_signal_action() - handle a signal
  454. *
  455. * @sig: signal
  456. * @pc: program counter
  457. */
  458. void os_signal_action(int sig, unsigned long pc);
  459. /**
  460. * os_get_time_offset() - get time offset
  461. *
  462. * Get the time offset from environment variable UBOOT_SB_TIME_OFFSET.
  463. *
  464. * Return: offset in seconds
  465. */
  466. long os_get_time_offset(void);
  467. /**
  468. * os_set_time_offset() - set time offset
  469. *
  470. * Save the time offset in environment variable UBOOT_SB_TIME_OFFSET.
  471. *
  472. * @offset: offset in seconds
  473. */
  474. void os_set_time_offset(long offset);
  475. #endif