183d290c5STom Rini /* SPDX-License-Identifier: GPL-2.0+ */ 27a9219c1SSimon Glass /* 3d9165153SSimon Glass * Operating System Interface 4d9165153SSimon Glass * 5d9165153SSimon Glass * This provides access to useful OS routines for the sandbox architecture. 6d9165153SSimon Glass * They are kept in a separate file so we can include system headers. 7d9165153SSimon Glass * 87a9219c1SSimon Glass * Copyright (c) 2011 The Chromium OS Authors. 97a9219c1SSimon Glass */ 107a9219c1SSimon Glass 114f345d56SMike Frysinger #ifndef __OS_H__ 124f345d56SMike Frysinger #define __OS_H__ 134f345d56SMike Frysinger 142a54d159SSimon Glass #include <linux/types.h> 152a54d159SSimon Glass 1694eefdeeSSimon Glass struct rtc_time; 1770db4212SSimon Glass struct sandbox_state; 1870db4212SSimon Glass 197a9219c1SSimon Glass /** 207a9219c1SSimon Glass * Access to the OS read() system call 217a9219c1SSimon Glass * 227a9219c1SSimon Glass * \param fd File descriptor as returned by os_open() 237a9219c1SSimon Glass * \param buf Buffer to place data 247a9219c1SSimon Glass * \param count Number of bytes to read 257a9219c1SSimon Glass * \return number of bytes read, or -1 on error 267a9219c1SSimon Glass */ 277a9219c1SSimon Glass ssize_t os_read(int fd, void *buf, size_t count); 287a9219c1SSimon Glass 297a9219c1SSimon Glass /** 307a9219c1SSimon Glass * Access to the OS write() system call 317a9219c1SSimon Glass * 327a9219c1SSimon Glass * \param fd File descriptor as returned by os_open() 337a9219c1SSimon Glass * \param buf Buffer containing data to write 347a9219c1SSimon Glass * \param count Number of bytes to write 357a9219c1SSimon Glass * \return number of bytes written, or -1 on error 367a9219c1SSimon Glass */ 377a9219c1SSimon Glass ssize_t os_write(int fd, const void *buf, size_t count); 387a9219c1SSimon Glass 397a9219c1SSimon Glass /** 40e2dcefcbSMike Frysinger * Access to the OS lseek() system call 41e2dcefcbSMike Frysinger * 42e2dcefcbSMike Frysinger * \param fd File descriptor as returned by os_open() 43e2dcefcbSMike Frysinger * \param offset File offset (based on whence) 44e2dcefcbSMike Frysinger * \param whence Position offset is relative to (see below) 45e2dcefcbSMike Frysinger * \return new file offset 46e2dcefcbSMike Frysinger */ 47e2dcefcbSMike Frysinger off_t os_lseek(int fd, off_t offset, int whence); 48e2dcefcbSMike Frysinger 49e2dcefcbSMike Frysinger /* Defines for "whence" in os_lseek() */ 50e2dcefcbSMike Frysinger #define OS_SEEK_SET 0 51e2dcefcbSMike Frysinger #define OS_SEEK_CUR 1 52e2dcefcbSMike Frysinger #define OS_SEEK_END 2 53e2dcefcbSMike Frysinger 54e2dcefcbSMike Frysinger /** 557a9219c1SSimon Glass * Access to the OS open() system call 567a9219c1SSimon Glass * 577a9219c1SSimon Glass * \param pathname Pathname of file to open 58887bd416SSimon Glass * \param flags Flags, like OS_O_RDONLY, OS_O_RDWR 597a9219c1SSimon Glass * \return file descriptor, or -1 on error 607a9219c1SSimon Glass */ 617a9219c1SSimon Glass int os_open(const char *pathname, int flags); 627a9219c1SSimon Glass 63d9165153SSimon Glass #define OS_O_RDONLY 0 64d9165153SSimon Glass #define OS_O_WRONLY 1 65d9165153SSimon Glass #define OS_O_RDWR 2 66d9165153SSimon Glass #define OS_O_MASK 3 /* Mask for read/write flags */ 67d9165153SSimon Glass #define OS_O_CREAT 0100 6850b288acSSimon Glass #define OS_O_TRUNC 01000 69d9165153SSimon Glass 707a9219c1SSimon Glass /** 717a9219c1SSimon Glass * Access to the OS close() system call 727a9219c1SSimon Glass * 737a9219c1SSimon Glass * \param fd File descriptor to close 747a9219c1SSimon Glass * \return 0 on success, -1 on error 757a9219c1SSimon Glass */ 767a9219c1SSimon Glass int os_close(int fd); 777a9219c1SSimon Glass 787a9219c1SSimon Glass /** 79cfd13e8dSStephen Warren * Access to the OS unlink() system call 80cfd13e8dSStephen Warren * 81cfd13e8dSStephen Warren * \param pathname Path of file to delete 82cfd13e8dSStephen Warren * \return 0 for success, other for error 83cfd13e8dSStephen Warren */ 84cfd13e8dSStephen Warren int os_unlink(const char *pathname); 85cfd13e8dSStephen Warren 86cfd13e8dSStephen Warren /** 877a9219c1SSimon Glass * Access to the OS exit() system call 887a9219c1SSimon Glass * 897a9219c1SSimon Glass * This exits with the supplied return code, which should be 0 to indicate 907a9219c1SSimon Glass * success. 917a9219c1SSimon Glass * 927a9219c1SSimon Glass * @param exit_code exit code for U-Boot 937a9219c1SSimon Glass */ 949d72e67bSMike Frysinger void os_exit(int exit_code) __attribute__((noreturn)); 95ab06a758SMike Frysinger 96ab06a758SMike Frysinger /** 97ab06a758SMike Frysinger * Put tty into raw mode to mimic serial console better 98ffb87905SSimon Glass * 99ffb87905SSimon Glass * @param fd File descriptor of stdin (normally 0) 100ffb87905SSimon Glass * @param allow_sigs Allow Ctrl-C, Ctrl-Z to generate signals rather than 101ffb87905SSimon Glass * be handled by U-Boot 102ab06a758SMike Frysinger */ 103ffb87905SSimon Glass void os_tty_raw(int fd, bool allow_sigs); 10421899b10SMatthias Weisser 10521899b10SMatthias Weisser /** 1068939df09SSimon Glass * Restore the tty to its original mode 1078939df09SSimon Glass * 1088939df09SSimon Glass * Call this to restore the original terminal mode, after it has been changed 1098939df09SSimon Glass * by os_tty_raw(). This is an internal function. 1108939df09SSimon Glass */ 1118939df09SSimon Glass void os_fd_restore(void); 1128939df09SSimon Glass 1138939df09SSimon Glass /** 11421899b10SMatthias Weisser * Acquires some memory from the underlying os. 11521899b10SMatthias Weisser * 11621899b10SMatthias Weisser * \param length Number of bytes to be allocated 11721899b10SMatthias Weisser * \return Pointer to length bytes or NULL on error 11821899b10SMatthias Weisser */ 11921899b10SMatthias Weisser void *os_malloc(size_t length); 120d99a6874SMatthias Weisser 121d99a6874SMatthias Weisser /** 12277595c6dSSimon Glass * Free memory previous allocated with os_malloc()/os_realloc() 12377595c6dSSimon Glass * 12477595c6dSSimon Glass * This returns the memory to the OS. 12577595c6dSSimon Glass * 12677595c6dSSimon Glass * \param ptr Pointer to memory block to free 12777595c6dSSimon Glass */ 128347d06deSMasahiro Yamada void os_free(void *ptr); 12977595c6dSSimon Glass 13077595c6dSSimon Glass /** 13177595c6dSSimon Glass * Reallocate previously-allocated memory to increase/decrease space 13277595c6dSSimon Glass * 13377595c6dSSimon Glass * This works in a similar way to the C library realloc() function. If 13477595c6dSSimon Glass * length is 0, then ptr is freed. Otherwise the space used by ptr is 13577595c6dSSimon Glass * expanded or reduced depending on whether length is larger or smaller 13677595c6dSSimon Glass * than before. 13777595c6dSSimon Glass * 13877595c6dSSimon Glass * If ptr is NULL, then this is similar to calling os_malloc(). 13977595c6dSSimon Glass * 14077595c6dSSimon Glass * This function may need to move the memory block to make room for any 14177595c6dSSimon Glass * extra space, in which case the new pointer is returned. 14277595c6dSSimon Glass * 14377595c6dSSimon Glass * \param ptr Pointer to memory block to reallocate 14477595c6dSSimon Glass * \param length New length for memory block 14577595c6dSSimon Glass * \return pointer to new memory block, or NULL on failure or if length 14677595c6dSSimon Glass * is 0. 14777595c6dSSimon Glass */ 14877595c6dSSimon Glass void *os_realloc(void *ptr, size_t length); 14977595c6dSSimon Glass 15077595c6dSSimon Glass /** 151d99a6874SMatthias Weisser * Access to the usleep function of the os 152d99a6874SMatthias Weisser * 153d99a6874SMatthias Weisser * \param usec Time to sleep in micro seconds 154d99a6874SMatthias Weisser */ 155d99a6874SMatthias Weisser void os_usleep(unsigned long usec); 156d99a6874SMatthias Weisser 157d99a6874SMatthias Weisser /** 158d99a6874SMatthias Weisser * Gets a monotonic increasing number of nano seconds from the OS 159d99a6874SMatthias Weisser * 160d99a6874SMatthias Weisser * \return A monotonic increasing time scaled in nano seconds 161d99a6874SMatthias Weisser */ 1622a54d159SSimon Glass uint64_t os_get_nsec(void); 1634f345d56SMike Frysinger 16470db4212SSimon Glass /** 16570db4212SSimon Glass * Parse arguments and update sandbox state. 16670db4212SSimon Glass * 16770db4212SSimon Glass * @param state Sandbox state to update 16870db4212SSimon Glass * @param argc Argument count 16970db4212SSimon Glass * @param argv Argument vector 17070db4212SSimon Glass * @return 0 if ok, and program should continue; 17170db4212SSimon Glass * 1 if ok, but program should stop; 17270db4212SSimon Glass * -1 on error: program should terminate. 17370db4212SSimon Glass */ 17470db4212SSimon Glass int os_parse_args(struct sandbox_state *state, int argc, char *argv[]); 17570db4212SSimon Glass 17662584db1SSimon Glass /* 17762584db1SSimon Glass * Types of directory entry that we support. See also os_dirent_typename in 17862584db1SSimon Glass * the C file. 17962584db1SSimon Glass */ 18062584db1SSimon Glass enum os_dirent_t { 18162584db1SSimon Glass OS_FILET_REG, /* Regular file */ 18262584db1SSimon Glass OS_FILET_LNK, /* Symbolic link */ 18362584db1SSimon Glass OS_FILET_DIR, /* Directory */ 18462584db1SSimon Glass OS_FILET_UNKNOWN, /* Something else */ 18562584db1SSimon Glass 18662584db1SSimon Glass OS_FILET_COUNT, 18762584db1SSimon Glass }; 18862584db1SSimon Glass 18962584db1SSimon Glass /** A directory entry node, containing information about a single dirent */ 19062584db1SSimon Glass struct os_dirent_node { 19162584db1SSimon Glass struct os_dirent_node *next; /* Pointer to next node, or NULL */ 19262584db1SSimon Glass ulong size; /* Size of file in bytes */ 19362584db1SSimon Glass enum os_dirent_t type; /* Type of entry */ 19462584db1SSimon Glass char name[0]; /* Name of entry */ 19562584db1SSimon Glass }; 19662584db1SSimon Glass 19762584db1SSimon Glass /** 19862584db1SSimon Glass * Get a directionry listing 19962584db1SSimon Glass * 20062584db1SSimon Glass * This allocates and returns a linked list containing the directory listing. 20162584db1SSimon Glass * 20262584db1SSimon Glass * @param dirname Directory to examine 20362584db1SSimon Glass * @param headp Returns pointer to head of linked list, or NULL if none 20462584db1SSimon Glass * @return 0 if ok, -ve on error 20562584db1SSimon Glass */ 20662584db1SSimon Glass int os_dirent_ls(const char *dirname, struct os_dirent_node **headp); 20762584db1SSimon Glass 20862584db1SSimon Glass /** 20986167089SStefan Brüns * Free directory list 21086167089SStefan Brüns * 21186167089SStefan Brüns * This frees a linked list containing a directory listing. 21286167089SStefan Brüns * 21386167089SStefan Brüns * @param node Pointer to head of linked list 21486167089SStefan Brüns */ 21586167089SStefan Brüns void os_dirent_free(struct os_dirent_node *node); 21686167089SStefan Brüns 21786167089SStefan Brüns /** 21862584db1SSimon Glass * Get the name of a directory entry type 21962584db1SSimon Glass * 22086167089SStefan Brüns * @param type Type to check 22162584db1SSimon Glass * @return string containing the name of that type, or "???" if none/invalid 22262584db1SSimon Glass */ 22362584db1SSimon Glass const char *os_dirent_get_typename(enum os_dirent_t type); 22462584db1SSimon Glass 22562584db1SSimon Glass /** 22662584db1SSimon Glass * Get the size of a file 22762584db1SSimon Glass * 22862584db1SSimon Glass * @param fname Filename to check 22996b1046dSSuriyan Ramasami * @param size size of file is returned if no error 23096b1046dSSuriyan Ramasami * @return 0 on success or -1 if an error ocurred 23162584db1SSimon Glass */ 23296b1046dSSuriyan Ramasami int os_get_filesize(const char *fname, loff_t *size); 23362584db1SSimon Glass 23491b136c7SSimon Glass /** 2350b189b6cSSimon Glass * Write a character to the controlling OS terminal 2360b189b6cSSimon Glass * 2370b189b6cSSimon Glass * This bypasses the U-Boot console support and writes directly to the OS 2380b189b6cSSimon Glass * stdout file descriptor. 2390b189b6cSSimon Glass * 2400b189b6cSSimon Glass * @param ch Character to write 2410b189b6cSSimon Glass */ 2420b189b6cSSimon Glass void os_putc(int ch); 2430b189b6cSSimon Glass 2440b189b6cSSimon Glass /** 2450b189b6cSSimon Glass * Write a string to the controlling OS terminal 2460b189b6cSSimon Glass * 2470b189b6cSSimon Glass * This bypasses the U-Boot console support and writes directly to the OS 2480b189b6cSSimon Glass * stdout file descriptor. 2490b189b6cSSimon Glass * 2500b189b6cSSimon Glass * @param str String to write (note that \n is not appended) 2510b189b6cSSimon Glass */ 2520b189b6cSSimon Glass void os_puts(const char *str); 2530b189b6cSSimon Glass 2540b189b6cSSimon Glass /** 2555c2859cdSSimon Glass * Write the sandbox RAM buffer to a existing file 2565c2859cdSSimon Glass * 2575c2859cdSSimon Glass * @param fname Filename to write memory to (simple binary format) 2585c2859cdSSimon Glass * @return 0 if OK, -ve on error 2595c2859cdSSimon Glass */ 2605c2859cdSSimon Glass int os_write_ram_buf(const char *fname); 2615c2859cdSSimon Glass 2625c2859cdSSimon Glass /** 2635c2859cdSSimon Glass * Read the sandbox RAM buffer from an existing file 2645c2859cdSSimon Glass * 2655c2859cdSSimon Glass * @param fname Filename containing memory (simple binary format) 2665c2859cdSSimon Glass * @return 0 if OK, -ve on error 2675c2859cdSSimon Glass */ 2685c2859cdSSimon Glass int os_read_ram_buf(const char *fname); 2695c2859cdSSimon Glass 27047f5fcfbSSimon Glass /** 27147f5fcfbSSimon Glass * Jump to a new executable image 27247f5fcfbSSimon Glass * 27347f5fcfbSSimon Glass * This uses exec() to run a new executable image, after putting it in a 27447f5fcfbSSimon Glass * temporary file. The same arguments and environment are passed to this 27547f5fcfbSSimon Glass * new image, with the addition of: 27647f5fcfbSSimon Glass * 27747f5fcfbSSimon Glass * -j <filename> Specifies the filename the image was written to. The 27847f5fcfbSSimon Glass * calling image may want to delete this at some point. 27947f5fcfbSSimon Glass * -m <filename> Specifies the file containing the sandbox memory 28047f5fcfbSSimon Glass * (ram_buf) from this image, so that the new image can 28147f5fcfbSSimon Glass * have access to this. It also means that the original 28247f5fcfbSSimon Glass * memory filename passed to U-Boot will be left intact. 28347f5fcfbSSimon Glass * 28447f5fcfbSSimon Glass * @param dest Buffer containing executable image 28547f5fcfbSSimon Glass * @param size Size of buffer 28647f5fcfbSSimon Glass */ 28747f5fcfbSSimon Glass int os_jump_to_image(const void *dest, int size); 28847f5fcfbSSimon Glass 28994eefdeeSSimon Glass /** 290d4e33f5aSSimon Glass * os_find_u_boot() - Determine the path to U-Boot proper 291d4e33f5aSSimon Glass * 292d4e33f5aSSimon Glass * This function is intended to be called from within sandbox SPL. It uses 293d4e33f5aSSimon Glass * a few heuristics to find U-Boot proper. Normally it is either in the same 294d4e33f5aSSimon Glass * directory, or the directory above (since u-boot-spl is normally in an 295d4e33f5aSSimon Glass * spl/ subdirectory when built). 296d4e33f5aSSimon Glass * 297d4e33f5aSSimon Glass * @fname: Place to put full path to U-Boot 298d4e33f5aSSimon Glass * @maxlen: Maximum size of @fname 299d4e33f5aSSimon Glass * @return 0 if OK, -NOSPC if the filename is too large, -ENOENT if not found 300d4e33f5aSSimon Glass */ 301d4e33f5aSSimon Glass int os_find_u_boot(char *fname, int maxlen); 302d4e33f5aSSimon Glass 303d4e33f5aSSimon Glass /** 304d4e33f5aSSimon Glass * os_spl_to_uboot() - Run U-Boot proper 305d4e33f5aSSimon Glass * 306d4e33f5aSSimon Glass * When called from SPL, this runs U-Boot proper. The filename is obtained by 307d4e33f5aSSimon Glass * calling os_find_u_boot(). 308d4e33f5aSSimon Glass * 309d4e33f5aSSimon Glass * @fname: Full pathname to U-Boot executable 310d4e33f5aSSimon Glass * @return 0 if OK, -ve on error 311d4e33f5aSSimon Glass */ 312d4e33f5aSSimon Glass int os_spl_to_uboot(const char *fname); 313d4e33f5aSSimon Glass 314d4e33f5aSSimon Glass /** 31594eefdeeSSimon Glass * Read the current system time 31694eefdeeSSimon Glass * 31794eefdeeSSimon Glass * This reads the current Local Time and places it into the provided 31894eefdeeSSimon Glass * structure. 31994eefdeeSSimon Glass * 32094eefdeeSSimon Glass * @param rt Place to put system time 32194eefdeeSSimon Glass */ 32294eefdeeSSimon Glass void os_localtime(struct rtc_time *rt); 32394eefdeeSSimon Glass 32430eef21fSSimon Glass /** 325fe938fb0SSimon Glass * os_abort() - Raise SIGABRT to exit sandbox (e.g. to debugger) 326fe938fb0SSimon Glass */ 327fe938fb0SSimon Glass void os_abort(void); 3289f8037eaSSimon Glass 3299f8037eaSSimon Glass /** 3309f8037eaSSimon Glass * os_mprotect_allow() - Remove write-protection on a region of memory 3319f8037eaSSimon Glass * 3329f8037eaSSimon Glass * The start and length will be page-aligned before use. 3339f8037eaSSimon Glass * 3349f8037eaSSimon Glass * @start: Region start 3359f8037eaSSimon Glass * @len: Region length in bytes 3369f8037eaSSimon Glass * @return 0 if OK, -1 on error from mprotect() 3379f8037eaSSimon Glass */ 3389f8037eaSSimon Glass int os_mprotect_allow(void *start, size_t len); 3399f8037eaSSimon Glass 340056a5ceaSSimon Glass /** 341056a5ceaSSimon Glass * os_write_file() - Write a file to the host filesystem 342056a5ceaSSimon Glass * 343056a5ceaSSimon Glass * This can be useful when debugging for writing data out of sandbox for 344056a5ceaSSimon Glass * inspection by external tools. 345056a5ceaSSimon Glass * 346056a5ceaSSimon Glass * @name: File path to write to 347056a5ceaSSimon Glass * @buf: Data to write 348056a5ceaSSimon Glass * @size: Size of data to write 349056a5ceaSSimon Glass * @return 0 if OK, -ve on error 350056a5ceaSSimon Glass */ 351056a5ceaSSimon Glass int os_write_file(const char *name, const void *buf, int size); 352056a5ceaSSimon Glass 353*566bf3a8SSimon Glass /** 354*566bf3a8SSimon Glass * os_read_file() - Read a file from the host filesystem 355*566bf3a8SSimon Glass * 356*566bf3a8SSimon Glass * This can be useful when reading test data into sandbox for use by test 357*566bf3a8SSimon Glass * routines. The data is allocated using os_malloc() and should be freed by 358*566bf3a8SSimon Glass * the caller. 359*566bf3a8SSimon Glass * 360*566bf3a8SSimon Glass * @name: File path to read from 361*566bf3a8SSimon Glass * @bufp: Returns buffer containing data read 362*566bf3a8SSimon Glass * @sizep: Returns size of data 363*566bf3a8SSimon Glass * @return 0 if OK, -ve on error 364*566bf3a8SSimon Glass */ 365*566bf3a8SSimon Glass int os_read_file(const char *name, void **bufp, int *sizep); 366*566bf3a8SSimon Glass 3674f345d56SMike Frysinger #endif 368