17a9219c1SSimon Glass /* 2d9165153SSimon Glass * Operating System Interface 3d9165153SSimon Glass * 4d9165153SSimon Glass * This provides access to useful OS routines for the sandbox architecture. 5d9165153SSimon Glass * They are kept in a separate file so we can include system headers. 6d9165153SSimon Glass * 77a9219c1SSimon Glass * Copyright (c) 2011 The Chromium OS Authors. 81a459660SWolfgang Denk * SPDX-License-Identifier: GPL-2.0+ 97a9219c1SSimon Glass */ 107a9219c1SSimon Glass 114f345d56SMike Frysinger #ifndef __OS_H__ 124f345d56SMike Frysinger #define __OS_H__ 134f345d56SMike Frysinger 142a54d159SSimon Glass #include <linux/types.h> 152a54d159SSimon Glass 1670db4212SSimon Glass struct sandbox_state; 1770db4212SSimon Glass 187a9219c1SSimon Glass /** 197a9219c1SSimon Glass * Access to the OS read() system call 207a9219c1SSimon Glass * 217a9219c1SSimon Glass * \param fd File descriptor as returned by os_open() 227a9219c1SSimon Glass * \param buf Buffer to place data 237a9219c1SSimon Glass * \param count Number of bytes to read 247a9219c1SSimon Glass * \return number of bytes read, or -1 on error 257a9219c1SSimon Glass */ 267a9219c1SSimon Glass ssize_t os_read(int fd, void *buf, size_t count); 277a9219c1SSimon Glass 287a9219c1SSimon Glass /** 29e101550aSTaylor Hutt * Access to the OS read() system call with non-blocking access 30e101550aSTaylor Hutt * 31e101550aSTaylor Hutt * \param fd File descriptor as returned by os_open() 32e101550aSTaylor Hutt * \param buf Buffer to place data 33e101550aSTaylor Hutt * \param count Number of bytes to read 34e101550aSTaylor Hutt * \return number of bytes read, or -1 on error 35e101550aSTaylor Hutt */ 36e101550aSTaylor Hutt ssize_t os_read_no_block(int fd, void *buf, size_t count); 37e101550aSTaylor Hutt 38e101550aSTaylor Hutt /** 397a9219c1SSimon Glass * Access to the OS write() system call 407a9219c1SSimon Glass * 417a9219c1SSimon Glass * \param fd File descriptor as returned by os_open() 427a9219c1SSimon Glass * \param buf Buffer containing data to write 437a9219c1SSimon Glass * \param count Number of bytes to write 447a9219c1SSimon Glass * \return number of bytes written, or -1 on error 457a9219c1SSimon Glass */ 467a9219c1SSimon Glass ssize_t os_write(int fd, const void *buf, size_t count); 477a9219c1SSimon Glass 487a9219c1SSimon Glass /** 49e2dcefcbSMike Frysinger * Access to the OS lseek() system call 50e2dcefcbSMike Frysinger * 51e2dcefcbSMike Frysinger * \param fd File descriptor as returned by os_open() 52e2dcefcbSMike Frysinger * \param offset File offset (based on whence) 53e2dcefcbSMike Frysinger * \param whence Position offset is relative to (see below) 54e2dcefcbSMike Frysinger * \return new file offset 55e2dcefcbSMike Frysinger */ 56e2dcefcbSMike Frysinger off_t os_lseek(int fd, off_t offset, int whence); 57e2dcefcbSMike Frysinger 58e2dcefcbSMike Frysinger /* Defines for "whence" in os_lseek() */ 59e2dcefcbSMike Frysinger #define OS_SEEK_SET 0 60e2dcefcbSMike Frysinger #define OS_SEEK_CUR 1 61e2dcefcbSMike Frysinger #define OS_SEEK_END 2 62e2dcefcbSMike Frysinger 63e2dcefcbSMike Frysinger /** 647a9219c1SSimon Glass * Access to the OS open() system call 657a9219c1SSimon Glass * 667a9219c1SSimon Glass * \param pathname Pathname of file to open 677a9219c1SSimon Glass * \param flags Flags, like O_RDONLY, O_RDWR 687a9219c1SSimon Glass * \return file descriptor, or -1 on error 697a9219c1SSimon Glass */ 707a9219c1SSimon Glass int os_open(const char *pathname, int flags); 717a9219c1SSimon Glass 72d9165153SSimon Glass #define OS_O_RDONLY 0 73d9165153SSimon Glass #define OS_O_WRONLY 1 74d9165153SSimon Glass #define OS_O_RDWR 2 75d9165153SSimon Glass #define OS_O_MASK 3 /* Mask for read/write flags */ 76d9165153SSimon Glass #define OS_O_CREAT 0100 77d9165153SSimon Glass 787a9219c1SSimon Glass /** 797a9219c1SSimon Glass * Access to the OS close() system call 807a9219c1SSimon Glass * 817a9219c1SSimon Glass * \param fd File descriptor to close 827a9219c1SSimon Glass * \return 0 on success, -1 on error 837a9219c1SSimon Glass */ 847a9219c1SSimon Glass int os_close(int fd); 857a9219c1SSimon Glass 867a9219c1SSimon Glass /** 87*cfd13e8dSStephen Warren * Access to the OS unlink() system call 88*cfd13e8dSStephen Warren * 89*cfd13e8dSStephen Warren * \param pathname Path of file to delete 90*cfd13e8dSStephen Warren * \return 0 for success, other for error 91*cfd13e8dSStephen Warren */ 92*cfd13e8dSStephen Warren int os_unlink(const char *pathname); 93*cfd13e8dSStephen Warren 94*cfd13e8dSStephen Warren /** 957a9219c1SSimon Glass * Access to the OS exit() system call 967a9219c1SSimon Glass * 977a9219c1SSimon Glass * This exits with the supplied return code, which should be 0 to indicate 987a9219c1SSimon Glass * success. 997a9219c1SSimon Glass * 1007a9219c1SSimon Glass * @param exit_code exit code for U-Boot 1017a9219c1SSimon Glass */ 1029d72e67bSMike Frysinger void os_exit(int exit_code) __attribute__((noreturn)); 103ab06a758SMike Frysinger 104ab06a758SMike Frysinger /** 105ab06a758SMike Frysinger * Put tty into raw mode to mimic serial console better 106ab06a758SMike Frysinger */ 107ab06a758SMike Frysinger void os_tty_raw(int fd); 10821899b10SMatthias Weisser 10921899b10SMatthias Weisser /** 11021899b10SMatthias Weisser * Acquires some memory from the underlying os. 11121899b10SMatthias Weisser * 11221899b10SMatthias Weisser * \param length Number of bytes to be allocated 11321899b10SMatthias Weisser * \return Pointer to length bytes or NULL on error 11421899b10SMatthias Weisser */ 11521899b10SMatthias Weisser void *os_malloc(size_t length); 116d99a6874SMatthias Weisser 117d99a6874SMatthias Weisser /** 11877595c6dSSimon Glass * Free memory previous allocated with os_malloc()/os_realloc() 11977595c6dSSimon Glass * 12077595c6dSSimon Glass * This returns the memory to the OS. 12177595c6dSSimon Glass * 12277595c6dSSimon Glass * \param ptr Pointer to memory block to free 12377595c6dSSimon Glass */ 124347d06deSMasahiro Yamada void os_free(void *ptr); 12577595c6dSSimon Glass 12677595c6dSSimon Glass /** 12777595c6dSSimon Glass * Reallocate previously-allocated memory to increase/decrease space 12877595c6dSSimon Glass * 12977595c6dSSimon Glass * This works in a similar way to the C library realloc() function. If 13077595c6dSSimon Glass * length is 0, then ptr is freed. Otherwise the space used by ptr is 13177595c6dSSimon Glass * expanded or reduced depending on whether length is larger or smaller 13277595c6dSSimon Glass * than before. 13377595c6dSSimon Glass * 13477595c6dSSimon Glass * If ptr is NULL, then this is similar to calling os_malloc(). 13577595c6dSSimon Glass * 13677595c6dSSimon Glass * This function may need to move the memory block to make room for any 13777595c6dSSimon Glass * extra space, in which case the new pointer is returned. 13877595c6dSSimon Glass * 13977595c6dSSimon Glass * \param ptr Pointer to memory block to reallocate 14077595c6dSSimon Glass * \param length New length for memory block 14177595c6dSSimon Glass * \return pointer to new memory block, or NULL on failure or if length 14277595c6dSSimon Glass * is 0. 14377595c6dSSimon Glass */ 14477595c6dSSimon Glass void *os_realloc(void *ptr, size_t length); 14577595c6dSSimon Glass 14677595c6dSSimon Glass /** 147d99a6874SMatthias Weisser * Access to the usleep function of the os 148d99a6874SMatthias Weisser * 149d99a6874SMatthias Weisser * \param usec Time to sleep in micro seconds 150d99a6874SMatthias Weisser */ 151d99a6874SMatthias Weisser void os_usleep(unsigned long usec); 152d99a6874SMatthias Weisser 153d99a6874SMatthias Weisser /** 154d99a6874SMatthias Weisser * Gets a monotonic increasing number of nano seconds from the OS 155d99a6874SMatthias Weisser * 156d99a6874SMatthias Weisser * \return A monotonic increasing time scaled in nano seconds 157d99a6874SMatthias Weisser */ 1582a54d159SSimon Glass uint64_t os_get_nsec(void); 1594f345d56SMike Frysinger 16070db4212SSimon Glass /** 16170db4212SSimon Glass * Parse arguments and update sandbox state. 16270db4212SSimon Glass * 16370db4212SSimon Glass * @param state Sandbox state to update 16470db4212SSimon Glass * @param argc Argument count 16570db4212SSimon Glass * @param argv Argument vector 16670db4212SSimon Glass * @return 0 if ok, and program should continue; 16770db4212SSimon Glass * 1 if ok, but program should stop; 16870db4212SSimon Glass * -1 on error: program should terminate. 16970db4212SSimon Glass */ 17070db4212SSimon Glass int os_parse_args(struct sandbox_state *state, int argc, char *argv[]); 17170db4212SSimon Glass 17262584db1SSimon Glass /* 17362584db1SSimon Glass * Types of directory entry that we support. See also os_dirent_typename in 17462584db1SSimon Glass * the C file. 17562584db1SSimon Glass */ 17662584db1SSimon Glass enum os_dirent_t { 17762584db1SSimon Glass OS_FILET_REG, /* Regular file */ 17862584db1SSimon Glass OS_FILET_LNK, /* Symbolic link */ 17962584db1SSimon Glass OS_FILET_DIR, /* Directory */ 18062584db1SSimon Glass OS_FILET_UNKNOWN, /* Something else */ 18162584db1SSimon Glass 18262584db1SSimon Glass OS_FILET_COUNT, 18362584db1SSimon Glass }; 18462584db1SSimon Glass 18562584db1SSimon Glass /** A directory entry node, containing information about a single dirent */ 18662584db1SSimon Glass struct os_dirent_node { 18762584db1SSimon Glass struct os_dirent_node *next; /* Pointer to next node, or NULL */ 18862584db1SSimon Glass ulong size; /* Size of file in bytes */ 18962584db1SSimon Glass enum os_dirent_t type; /* Type of entry */ 19062584db1SSimon Glass char name[0]; /* Name of entry */ 19162584db1SSimon Glass }; 19262584db1SSimon Glass 19362584db1SSimon Glass /** 19462584db1SSimon Glass * Get a directionry listing 19562584db1SSimon Glass * 19662584db1SSimon Glass * This allocates and returns a linked list containing the directory listing. 19762584db1SSimon Glass * 19862584db1SSimon Glass * @param dirname Directory to examine 19962584db1SSimon Glass * @param headp Returns pointer to head of linked list, or NULL if none 20062584db1SSimon Glass * @return 0 if ok, -ve on error 20162584db1SSimon Glass */ 20262584db1SSimon Glass int os_dirent_ls(const char *dirname, struct os_dirent_node **headp); 20362584db1SSimon Glass 20462584db1SSimon Glass /** 20562584db1SSimon Glass * Get the name of a directory entry type 20662584db1SSimon Glass * 20762584db1SSimon Glass * @param type Type to cehck 20862584db1SSimon Glass * @return string containing the name of that type, or "???" if none/invalid 20962584db1SSimon Glass */ 21062584db1SSimon Glass const char *os_dirent_get_typename(enum os_dirent_t type); 21162584db1SSimon Glass 21262584db1SSimon Glass /** 21362584db1SSimon Glass * Get the size of a file 21462584db1SSimon Glass * 21562584db1SSimon Glass * @param fname Filename to check 21662584db1SSimon Glass * @return size of file, or -1 if an error ocurred 21762584db1SSimon Glass */ 21862584db1SSimon Glass ssize_t os_get_filesize(const char *fname); 21962584db1SSimon Glass 22091b136c7SSimon Glass /** 22191b136c7SSimon Glass * Write a character to the controlling OS terminal 22291b136c7SSimon Glass * 22391b136c7SSimon Glass * This bypasses the U-Boot console support and writes directly to the OS 22491b136c7SSimon Glass * stdout file descriptor. 22591b136c7SSimon Glass * 22691b136c7SSimon Glass * @param ch Character to write 22791b136c7SSimon Glass */ 22891b136c7SSimon Glass void os_putc(int ch); 22991b136c7SSimon Glass 23091b136c7SSimon Glass /** 23191b136c7SSimon Glass * Write a string to the controlling OS terminal 23291b136c7SSimon Glass * 23391b136c7SSimon Glass * This bypasses the U-Boot console support and writes directly to the OS 23491b136c7SSimon Glass * stdout file descriptor. 23591b136c7SSimon Glass * 23691b136c7SSimon Glass * @param str String to write (note that \n is not appended) 23791b136c7SSimon Glass */ 23891b136c7SSimon Glass void os_puts(const char *str); 23991b136c7SSimon Glass 2405c2859cdSSimon Glass /** 2415c2859cdSSimon Glass * Write the sandbox RAM buffer to a existing file 2425c2859cdSSimon Glass * 2435c2859cdSSimon Glass * @param fname Filename to write memory to (simple binary format) 2445c2859cdSSimon Glass * @return 0 if OK, -ve on error 2455c2859cdSSimon Glass */ 2465c2859cdSSimon Glass int os_write_ram_buf(const char *fname); 2475c2859cdSSimon Glass 2485c2859cdSSimon Glass /** 2495c2859cdSSimon Glass * Read the sandbox RAM buffer from an existing file 2505c2859cdSSimon Glass * 2515c2859cdSSimon Glass * @param fname Filename containing memory (simple binary format) 2525c2859cdSSimon Glass * @return 0 if OK, -ve on error 2535c2859cdSSimon Glass */ 2545c2859cdSSimon Glass int os_read_ram_buf(const char *fname); 2555c2859cdSSimon Glass 2564f345d56SMike Frysinger #endif 257