Initial commit
This commit is contained in:
commit
b5f785836d
30 changed files with 4773 additions and 0 deletions
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
|
@ -0,0 +1,5 @@
|
|||
*.so
|
||||
*.efi
|
||||
*.o
|
||||
*.elf
|
||||
bin/*
|
121
LICENSE
Normal file
121
LICENSE
Normal file
|
@ -0,0 +1,121 @@
|
|||
Creative Commons Legal Code
|
||||
|
||||
CC0 1.0 Universal
|
||||
|
||||
CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE
|
||||
LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN
|
||||
ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS
|
||||
INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES
|
||||
REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS
|
||||
PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM
|
||||
THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED
|
||||
HEREUNDER.
|
||||
|
||||
Statement of Purpose
|
||||
|
||||
The laws of most jurisdictions throughout the world automatically confer
|
||||
exclusive Copyright and Related Rights (defined below) upon the creator
|
||||
and subsequent owner(s) (each and all, an "owner") of an original work of
|
||||
authorship and/or a database (each, a "Work").
|
||||
|
||||
Certain owners wish to permanently relinquish those rights to a Work for
|
||||
the purpose of contributing to a commons of creative, cultural and
|
||||
scientific works ("Commons") that the public can reliably and without fear
|
||||
of later claims of infringement build upon, modify, incorporate in other
|
||||
works, reuse and redistribute as freely as possible in any form whatsoever
|
||||
and for any purposes, including without limitation commercial purposes.
|
||||
These owners may contribute to the Commons to promote the ideal of a free
|
||||
culture and the further production of creative, cultural and scientific
|
||||
works, or to gain reputation or greater distribution for their Work in
|
||||
part through the use and efforts of others.
|
||||
|
||||
For these and/or other purposes and motivations, and without any
|
||||
expectation of additional consideration or compensation, the person
|
||||
associating CC0 with a Work (the "Affirmer"), to the extent that he or she
|
||||
is an owner of Copyright and Related Rights in the Work, voluntarily
|
||||
elects to apply CC0 to the Work and publicly distribute the Work under its
|
||||
terms, with knowledge of his or her Copyright and Related Rights in the
|
||||
Work and the meaning and intended legal effect of CC0 on those rights.
|
||||
|
||||
1. Copyright and Related Rights. A Work made available under CC0 may be
|
||||
protected by copyright and related or neighboring rights ("Copyright and
|
||||
Related Rights"). Copyright and Related Rights include, but are not
|
||||
limited to, the following:
|
||||
|
||||
i. the right to reproduce, adapt, distribute, perform, display,
|
||||
communicate, and translate a Work;
|
||||
ii. moral rights retained by the original author(s) and/or performer(s);
|
||||
iii. publicity and privacy rights pertaining to a person's image or
|
||||
likeness depicted in a Work;
|
||||
iv. rights protecting against unfair competition in regards to a Work,
|
||||
subject to the limitations in paragraph 4(a), below;
|
||||
v. rights protecting the extraction, dissemination, use and reuse of data
|
||||
in a Work;
|
||||
vi. database rights (such as those arising under Directive 96/9/EC of the
|
||||
European Parliament and of the Council of 11 March 1996 on the legal
|
||||
protection of databases, and under any national implementation
|
||||
thereof, including any amended or successor version of such
|
||||
directive); and
|
||||
vii. other similar, equivalent or corresponding rights throughout the
|
||||
world based on applicable law or treaty, and any national
|
||||
implementations thereof.
|
||||
|
||||
2. Waiver. To the greatest extent permitted by, but not in contravention
|
||||
of, applicable law, Affirmer hereby overtly, fully, permanently,
|
||||
irrevocably and unconditionally waives, abandons, and surrenders all of
|
||||
Affirmer's Copyright and Related Rights and associated claims and causes
|
||||
of action, whether now known or unknown (including existing as well as
|
||||
future claims and causes of action), in the Work (i) in all territories
|
||||
worldwide, (ii) for the maximum duration provided by applicable law or
|
||||
treaty (including future time extensions), (iii) in any current or future
|
||||
medium and for any number of copies, and (iv) for any purpose whatsoever,
|
||||
including without limitation commercial, advertising or promotional
|
||||
purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each
|
||||
member of the public at large and to the detriment of Affirmer's heirs and
|
||||
successors, fully intending that such Waiver shall not be subject to
|
||||
revocation, rescission, cancellation, termination, or any other legal or
|
||||
equitable action to disrupt the quiet enjoyment of the Work by the public
|
||||
as contemplated by Affirmer's express Statement of Purpose.
|
||||
|
||||
3. Public License Fallback. Should any part of the Waiver for any reason
|
||||
be judged legally invalid or ineffective under applicable law, then the
|
||||
Waiver shall be preserved to the maximum extent permitted taking into
|
||||
account Affirmer's express Statement of Purpose. In addition, to the
|
||||
extent the Waiver is so judged Affirmer hereby grants to each affected
|
||||
person a royalty-free, non transferable, non sublicensable, non exclusive,
|
||||
irrevocable and unconditional license to exercise Affirmer's Copyright and
|
||||
Related Rights in the Work (i) in all territories worldwide, (ii) for the
|
||||
maximum duration provided by applicable law or treaty (including future
|
||||
time extensions), (iii) in any current or future medium and for any number
|
||||
of copies, and (iv) for any purpose whatsoever, including without
|
||||
limitation commercial, advertising or promotional purposes (the
|
||||
"License"). The License shall be deemed effective as of the date CC0 was
|
||||
applied by Affirmer to the Work. Should any part of the License for any
|
||||
reason be judged legally invalid or ineffective under applicable law, such
|
||||
partial invalidity or ineffectiveness shall not invalidate the remainder
|
||||
of the License, and in such case Affirmer hereby affirms that he or she
|
||||
will not (i) exercise any of his or her remaining Copyright and Related
|
||||
Rights in the Work or (ii) assert any associated claims and causes of
|
||||
action with respect to the Work, in either case contrary to Affirmer's
|
||||
express Statement of Purpose.
|
||||
|
||||
4. Limitations and Disclaimers.
|
||||
|
||||
a. No trademark or patent rights held by Affirmer are waived, abandoned,
|
||||
surrendered, licensed or otherwise affected by this document.
|
||||
b. Affirmer offers the Work as-is and makes no representations or
|
||||
warranties of any kind concerning the Work, express, implied,
|
||||
statutory or otherwise, including without limitation warranties of
|
||||
title, merchantability, fitness for a particular purpose, non
|
||||
infringement, or the absence of latent or other defects, accuracy, or
|
||||
the present or absence of errors, whether or not discoverable, all to
|
||||
the greatest extent permissible under applicable law.
|
||||
c. Affirmer disclaims responsibility for clearing rights of other persons
|
||||
that may apply to the Work or any use thereof, including without
|
||||
limitation any person's Copyright and Related Rights in the Work.
|
||||
Further, Affirmer disclaims responsibility for obtaining any necessary
|
||||
consents, permissions or other rights required for any use of the
|
||||
Work.
|
||||
d. Affirmer understands and acknowledges that Creative Commons is not a
|
||||
party to this document and has no duty or obligation with respect to
|
||||
this CC0 or use of the Work.
|
23
Makefile
Normal file
23
Makefile
Normal file
|
@ -0,0 +1,23 @@
|
|||
all: link
|
||||
compile: obj/linux-uefi-c.o obj/main-c.o
|
||||
link: compile
|
||||
ld -o bin/efitest -m elf_x86_64 obj/linux-uefi-c.o obj/main-c.o -znocombreloc -T /usr/lib/elf_x86_64_efi.lds -shared -Bsymbolic -L /usr/lib /usr/lib/crt0-efi-x86_64.o -lefi -lgnuefi -nostdlib
|
||||
clean:
|
||||
rm -f obj/linux-uefi-c.o obj/main-c.o bin/efitest
|
||||
|
||||
|
||||
obj/linux-uefi-c.o: src/linux-uefi.c
|
||||
gcc -DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch -Iinclude -Iinclude/compat -Inolibc -I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol -c src/linux-uefi.c -o obj/linux-uefi-c.o
|
||||
|
||||
obj/main-c.o: src/main.c
|
||||
gcc -DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch -Iinclude -Iinclude/compat -Inolibc -I/usr/include/efi -I/usr/include/efi/x86_64 -I/usr/include/efi/protocol -c src/main.c -o obj/main-c.o
|
||||
|
||||
efi: bin/efitest.efi
|
||||
|
||||
bin/efitest.efi: all
|
||||
objcopy -j .text -j .sdata -j .data -j .dynamic \
|
||||
-j .dynsym -j .rel -j .rela -j .reloc \
|
||||
--target=efi-app-x86_64 bin/efitest bin/efitest.efi
|
||||
|
||||
test: bin/efitest.efi
|
||||
qemu-system-x86_64 -bios /usr/share/ovmf/OVMF.fd -kernel bin/efitest.efi -m 128M
|
9
Makefile_cst
Normal file
9
Makefile_cst
Normal file
|
@ -0,0 +1,9 @@
|
|||
efi: bin/efitest.efi
|
||||
|
||||
bin/efitest.efi: all
|
||||
objcopy -j .text -j .sdata -j .data -j .dynamic \
|
||||
-j .dynsym -j .rel -j .rela -j .reloc \
|
||||
--target=efi-app-x86_64 bin/efitest bin/efitest.efi
|
||||
|
||||
test: bin/efitest.efi
|
||||
qemu-system-x86_64 -bios /usr/share/ovmf/OVMF.fd -kernel bin/efitest.efi -m 128M
|
27
README.md
Normal file
27
README.md
Normal file
|
@ -0,0 +1,27 @@
|
|||
# UEFI Linux
|
||||
No, this projects aim is *not* booting Linux from UEFI (which is already possible), but instead to <u>emulate</u> basic Linux system calls within the UEFI environment.
|
||||
|
||||
## Why???
|
||||
For fun. Nothing else. It this project would, in theory, eventually allow you to use any Linux C library in the UEFI environment.
|
||||
|
||||
## Status
|
||||
|
||||
Implemented:
|
||||
- Console I/O
|
||||
- File I/O
|
||||
- Directory I/O
|
||||
- Basic memory management (mmap/munmap)
|
||||
- Time keeping (gettimeofday/select)
|
||||
- Exit/Reboot/Poweroff
|
||||
- Basic signals (`getpid()` returns a PID that can be `kill()`ed)
|
||||
- System call emulation through function call
|
||||
|
||||
Planned:
|
||||
- Networking
|
||||
|
||||
Won't happen:
|
||||
- Multhreading
|
||||
- `syscall` instruction emulation
|
||||
|
||||
## Misc
|
||||
This is purely a showcase, not a library meant for production use. I will give no support of any kind, but I'm happy to answer any development related questions you may have.
|
1
include/compat/stddef.h
Symbolic link
1
include/compat/stddef.h
Symbolic link
|
@ -0,0 +1 @@
|
|||
linux-uefi-int.h
|
51
include/getdelim.h
Normal file
51
include/getdelim.h
Normal file
|
@ -0,0 +1,51 @@
|
|||
#ifndef _GETDELIM_H
|
||||
#define _GETDELIM_H
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
|
||||
static __attribute__((unused))
|
||||
ssize_t my_getdelim(char **out, size_t *out_size, int delim, int in)
|
||||
{
|
||||
size_t out_idx = 0;
|
||||
// Flush stdout
|
||||
fflush(stdout);
|
||||
// Allocate buffer if needed
|
||||
if (*out == NULL) {
|
||||
*out = malloc(2);
|
||||
*out_size = 1;
|
||||
}
|
||||
// Loop until delimiter
|
||||
for (;;) {
|
||||
// Read single character
|
||||
char c;
|
||||
if (read(in, &c, 1) <= 0) {
|
||||
return -1;
|
||||
}
|
||||
// Add it to buffer
|
||||
if (*out_size < out_idx + 1) {
|
||||
(*out_size)++;
|
||||
*out = realloc(*out, *out_size + 1);
|
||||
}
|
||||
(*out)[out_idx++] = c;
|
||||
// Break on delimiter
|
||||
if (c == (char)delim) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
// Add null terminator
|
||||
(*out)[out_idx] = '\0';
|
||||
// Return success
|
||||
return out_idx + 1;
|
||||
}
|
||||
|
||||
static inline __attribute__((unused))
|
||||
ssize_t my_getline(char **out, size_t *out_size, int in)
|
||||
{
|
||||
return my_getdelim(out, out_size, '\n', in);
|
||||
}
|
||||
|
||||
#endif /* _GETDELIM_H */
|
33
include/linux-uefi-int.h
Normal file
33
include/linux-uefi-int.h
Normal file
|
@ -0,0 +1,33 @@
|
|||
#ifndef _LINUX_UEFI_INT_H
|
||||
#define _LINUX_UEFI_INT_H
|
||||
|
||||
#ifdef LINUX_UEFI_USE_INTERNAL_INTS
|
||||
#ifndef NULL
|
||||
#define NULL ((void*)0)
|
||||
#endif /* NULL */
|
||||
|
||||
#ifndef __cplusplus
|
||||
typedef unsigned short wchar_t;
|
||||
#endif /* __cplusplus */
|
||||
|
||||
typedef unsigned long size_t;
|
||||
typedef signed long ssize_t;
|
||||
typedef signed long time_t;
|
||||
typedef signed long off_t;
|
||||
typedef signed int pid_t;
|
||||
typedef unsigned int uid_t;
|
||||
typedef unsigned int gid_t;
|
||||
typedef unsigned int dev_t;
|
||||
typedef unsigned long ino_t;
|
||||
typedef unsigned int mode_t;
|
||||
typedef unsigned long nlink_t;
|
||||
typedef signed long blksize_t;
|
||||
typedef signed long blksize_t;
|
||||
typedef signed long blkcnt_t;
|
||||
typedef unsigned long clock_t;
|
||||
typedef unsigned long nfds_t;
|
||||
#else
|
||||
# include <sys/types.h>
|
||||
#endif /* LINUX_UEFI_USE_INTERNAL_INTS */
|
||||
|
||||
#endif /* _LINUX_UEFI_INT_H */
|
37
include/linux-uefi-syscalls.h
Normal file
37
include/linux-uefi-syscalls.h
Normal file
|
@ -0,0 +1,37 @@
|
|||
#ifndef _LINUX_UEFI_SYSCALLS_H
|
||||
#define _LINUX_UEFI_SYSCALLS_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "linux-uefi-int.h"
|
||||
|
||||
|
||||
struct timeval;
|
||||
struct linux_dirent64;
|
||||
struct stat;
|
||||
|
||||
void proc_exit(int status);
|
||||
void proc_abort();
|
||||
|
||||
int power_reboot(int magic1, int magic2, int cmd, void *arg);
|
||||
|
||||
int time_gettimeofday(struct timeval *restrict tv);
|
||||
int time_sleep(struct timeval *duration);
|
||||
|
||||
int fd_open(const char *pathname, int flags);
|
||||
int fd_close(int fd);
|
||||
int fd_fsync(int fd);
|
||||
int fd_dup(int old_fd);
|
||||
int fd_dup2(int old_fd, int new_fd);
|
||||
off_t fd_lseek(int fd, off_t offset, int whence);
|
||||
ssize_t fd_read(int fd, void *buf, size_t count);
|
||||
ssize_t fd_write(int fd, const void *buf, size_t count);
|
||||
int fd_getdents64(int fd, struct linux_dirent64 *dirp, int count);
|
||||
int fd_fstat(int fd, struct stat *buf);
|
||||
int fd_stat(const char *path, struct stat *buf);
|
||||
|
||||
void *mem_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset);
|
||||
int mem_munmap(void *addr, size_t length);
|
||||
|
||||
uint64_t syscall_emu_x86_64(uint64_t no, uint64_t *args);
|
||||
|
||||
#endif /* _LINUX_UEFI_SYSCALLS_H */
|
109
include/linux-uefi.h
Normal file
109
include/linux-uefi.h
Normal file
|
@ -0,0 +1,109 @@
|
|||
#ifndef _LINUX_UEFI_H
|
||||
#define _LINUX_UEFI_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include "linux-uefi-int.h"
|
||||
|
||||
|
||||
/* Types */
|
||||
struct timeval {
|
||||
time_t tv_sec; /* seconds */
|
||||
long long tv_usec; /* microseconds */
|
||||
};
|
||||
|
||||
struct timespec {
|
||||
time_t tv_sec; /* seconds */
|
||||
time_t tv_nsec; /* nanoseconds */
|
||||
};
|
||||
|
||||
struct timezone {
|
||||
int tz_minuteswest; /* minutes west of Greenwich */
|
||||
int tz_dsttime; /* type of dst correction */
|
||||
};
|
||||
|
||||
struct linux_dirent64 {
|
||||
uint64_t d_ino;
|
||||
int64_t d_off;
|
||||
unsigned short d_reclen;
|
||||
unsigned char d_type;
|
||||
char d_name[];
|
||||
};
|
||||
|
||||
struct stat {
|
||||
unsigned long st_dev;
|
||||
unsigned long st_ino;
|
||||
unsigned long st_nlink;
|
||||
unsigned int st_mode;
|
||||
unsigned int st_uid;
|
||||
|
||||
unsigned int st_gid;
|
||||
unsigned int __pad0;
|
||||
unsigned long st_rdev;
|
||||
long st_size;
|
||||
long st_blksize;
|
||||
|
||||
long st_blocks;
|
||||
union { time_t st_atime; struct timespec st_atim; };
|
||||
union { time_t st_mtime; struct timespec st_mtim; };
|
||||
union { time_t st_ctime; struct timespec st_ctim; };
|
||||
long __unused[3];
|
||||
};
|
||||
|
||||
|
||||
/* Macros */
|
||||
#define LINUX_REBOOT_MAGIC1 0xfee1dead
|
||||
#define LINUX_REBOOT_MAGIC2 0x28121969
|
||||
#define LINUX_REBOOT_MAGIC2A 0x05121996
|
||||
#define LINUX_REBOOT_MAGIC2B 0x16041998
|
||||
#define LINUX_REBOOT_MAGIC2C 0x20112000
|
||||
|
||||
#define LINUX_REBOOT_CMD_CAD_OFF 0
|
||||
#define LINUX_REBOOT_CMD_CAD_ON 0x89abcdef
|
||||
#define LINUX_REBOOT_CMD_HALT 0xcdef0123
|
||||
#define LINUX_REBOOT_CMD_KEXEC 0x45584543
|
||||
#define LINUX_REBOOT_CMD_POWER_OFF 0x4321fedc
|
||||
#define LINUX_REBOOT_CMD_RESTART 0x1234567
|
||||
#define LINUX_REBOOT_CMD_RESTART2 0xa1b2c3d4
|
||||
#define LINUX_REBOOT_CMD_SW_SUSPEND 0xd000fce1
|
||||
|
||||
#define EPERM 1 /* Operation not permitted */
|
||||
#define ENOENT 2 /* No such file or directory */
|
||||
#define ESRCH 3 /* No such process */
|
||||
#define EIO 5 /* I/O error */
|
||||
#define ENXIO 6 /* No such device or address */
|
||||
#define E2BIG 7 /* Argument list too long */
|
||||
#define EBADF 9 /* Bad file number */
|
||||
#define ENOMEM 12 /* Out of memory */
|
||||
#define EACCES 13 /* Permission denied */
|
||||
#define EFAULT 14 /* Bad address */
|
||||
#define ENOTBLK 15 /* Block device required */
|
||||
#define EEXIST 17 /* File exists */
|
||||
#define ENODEV 19 /* No such device */
|
||||
#define ENOTDIR 20 /* Not a directory */
|
||||
#define EISDIR 21 /* Is a directory */
|
||||
#define EINVAL 22 /* Invalid argument */
|
||||
#define EMFILE 24 /* Too many open files */
|
||||
#define ENOTTY 25 /* Not a typewriter */
|
||||
#define EFBIG 27 /* File too large */
|
||||
#define ENOSPC 28 /* No space left on device */
|
||||
#define ESPIPE 29 /* Illegal seek */
|
||||
#define EROFS 30 /* Read-only file system */
|
||||
#define ENAMETOOLONG 36 /* File name too long */
|
||||
#define ENOSYS 38 /* Invalid system call number */
|
||||
#define ENOTSUP 95 /* Operation not supported */
|
||||
|
||||
#define SIGABRT 6 /* Abort */
|
||||
|
||||
#define O_RDONLY 00000000
|
||||
#define O_WRONLY 00000001
|
||||
#define O_RDWR 00000002
|
||||
|
||||
#define O_CREAT 00000100 /* not fcntl */
|
||||
#define O_APPEND 00002000
|
||||
#define O_DIRECTORY 00200000 /* must be a directory */
|
||||
|
||||
#define SEEK_SET 0 /* seek relative to beginning of file */
|
||||
#define SEEK_CUR 1 /* seek relative to current file position */
|
||||
#define SEEK_END 2 /* seek relative to end of file */
|
||||
|
||||
#endif /* _LINUX_UEFI_H */
|
82
nolibc/arch-x86_64.h
Normal file
82
nolibc/arch-x86_64.h
Normal file
|
@ -0,0 +1,82 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
|
||||
/*
|
||||
* x86_64 specific definitions for NOLIBC
|
||||
* Copyright (C) 2017-2022 Willy Tarreau <w@1wt.eu>
|
||||
*/
|
||||
|
||||
#ifndef _NOLIBC_ARCH_X86_64_H
|
||||
#define _NOLIBC_ARCH_X86_64_H
|
||||
|
||||
#include "compiler.h"
|
||||
#include "linux-uefi-syscalls.h"
|
||||
|
||||
/* The struct returned by the stat() syscall, equivalent to stat64(). The
|
||||
* syscall returns 116 bytes and stops in the middle of __unused.
|
||||
*/
|
||||
struct sys_stat_struct {
|
||||
unsigned long st_dev;
|
||||
unsigned long st_ino;
|
||||
unsigned long st_nlink;
|
||||
unsigned int st_mode;
|
||||
unsigned int st_uid;
|
||||
|
||||
unsigned int st_gid;
|
||||
unsigned int __pad0;
|
||||
unsigned long st_rdev;
|
||||
long st_size;
|
||||
long st_blksize;
|
||||
|
||||
long st_blocks;
|
||||
unsigned long st_atime;
|
||||
unsigned long st_atime_nsec;
|
||||
unsigned long st_mtime;
|
||||
|
||||
unsigned long st_mtime_nsec;
|
||||
unsigned long st_ctime;
|
||||
unsigned long st_ctime_nsec;
|
||||
long __unused[3];
|
||||
};
|
||||
|
||||
/* Syscalls for x86_64 :
|
||||
* - registers are 64-bit
|
||||
* - syscall number is passed in rax
|
||||
* - arguments are in rdi, rsi, rdx, r10, r8, r9 respectively
|
||||
* - the system call is performed by calling the syscall instruction
|
||||
* - syscall return comes in rax
|
||||
* - rcx and r11 are clobbered, others are preserved.
|
||||
* - the arguments are cast to long and assigned into the target registers
|
||||
* which are then simply passed as registers to the asm code, so that we
|
||||
* don't have to experience issues with register constraints.
|
||||
* - the syscall number is always specified last in order to allow to force
|
||||
* some registers before (gcc refuses a %-register at the last position).
|
||||
* - see also x86-64 ABI section A.2 AMD64 Linux Kernel Conventions, A.2.1
|
||||
* Calling Conventions.
|
||||
*
|
||||
* Link x86-64 ABI: https://gitlab.com/x86-psABIs/x86-64-ABI/-/wikis/home
|
||||
*
|
||||
*/
|
||||
|
||||
#define my_syscall0(num) \
|
||||
({ \
|
||||
uint64_t args[] = {0}; \
|
||||
long _ret = syscall_emu_x86_64(num, args); \
|
||||
_ret; \
|
||||
})
|
||||
|
||||
#define my_syscall1(num, ...) \
|
||||
({ \
|
||||
uint64_t args[] = {__VA_ARGS__}; \
|
||||
long _ret = syscall_emu_x86_64(num, args); \
|
||||
_ret; \
|
||||
})
|
||||
|
||||
#define my_syscall2 my_syscall1
|
||||
#define my_syscall3 my_syscall1
|
||||
#define my_syscall4 my_syscall1
|
||||
#define my_syscall5 my_syscall1
|
||||
#define my_syscall6 my_syscall1
|
||||
|
||||
char **environ __attribute__((weak));
|
||||
const unsigned long *_auxv __attribute__((weak));
|
||||
|
||||
#endif /* _NOLIBC_ARCH_X86_64_H */
|
20
nolibc/arch.h
Normal file
20
nolibc/arch.h
Normal file
|
@ -0,0 +1,20 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
|
||||
/*
|
||||
* Copyright (C) 2017-2022 Willy Tarreau <w@1wt.eu>
|
||||
*/
|
||||
|
||||
/* Below comes the architecture-specific code. For each architecture, we have
|
||||
* the syscall declarations and the _start code definition. This is the only
|
||||
* global part. On all architectures the kernel puts everything in the stack
|
||||
* before jumping to _start just above us, without any return address (_start
|
||||
* is not a function but an entry point). So at the stack pointer we find argc.
|
||||
* Then argv[] begins, and ends at the first NULL. Then we have envp which
|
||||
* starts and ends with a NULL as well. So envp=argv+argc+1.
|
||||
*/
|
||||
|
||||
#ifndef _NOLIBC_ARCH_H
|
||||
#define _NOLIBC_ARCH_H
|
||||
|
||||
#include "arch-x86_64.h"
|
||||
|
||||
#endif /* _NOLIBC_ARCH_H */
|
25
nolibc/compiler.h
Normal file
25
nolibc/compiler.h
Normal file
|
@ -0,0 +1,25 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
|
||||
/*
|
||||
* NOLIBC compiler support header
|
||||
* Copyright (C) 2023 Thomas Weißschuh <linux@weissschuh.net>
|
||||
*/
|
||||
#ifndef _NOLIBC_COMPILER_H
|
||||
#define _NOLIBC_COMPILER_H
|
||||
|
||||
#if defined(__SSP__) || defined(__SSP_STRONG__) || defined(__SSP_ALL__) || defined(__SSP_EXPLICIT__)
|
||||
|
||||
#define _NOLIBC_STACKPROTECTOR
|
||||
|
||||
#endif /* defined(__SSP__) ... */
|
||||
|
||||
#if defined(__has_attribute)
|
||||
# if __has_attribute(no_stack_protector)
|
||||
# define __no_stack_protector __attribute__((no_stack_protector))
|
||||
# else
|
||||
# define __no_stack_protector __attribute__((__optimize__("-fno-stack-protector")))
|
||||
# endif
|
||||
#else
|
||||
# define __no_stack_protector __attribute__((__optimize__("-fno-stack-protector")))
|
||||
#endif /* defined(__has_attribute) */
|
||||
|
||||
#endif /* _NOLIBC_COMPILER_H */
|
102
nolibc/ctype.h
Normal file
102
nolibc/ctype.h
Normal file
|
@ -0,0 +1,102 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
|
||||
/*
|
||||
* ctype function definitions for NOLIBC
|
||||
* Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
|
||||
*/
|
||||
|
||||
#ifndef _NOLIBC_CTYPE_H
|
||||
#define _NOLIBC_CTYPE_H
|
||||
|
||||
#include "std.h"
|
||||
|
||||
/*
|
||||
* As much as possible, please keep functions alphabetically sorted.
|
||||
*/
|
||||
|
||||
static __attribute__((unused))
|
||||
int isascii(int c)
|
||||
{
|
||||
/* 0x00..0x7f */
|
||||
return (unsigned int)c <= 0x7f;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
int isblank(int c)
|
||||
{
|
||||
return c == '\t' || c == ' ';
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
int iscntrl(int c)
|
||||
{
|
||||
/* 0x00..0x1f, 0x7f */
|
||||
return (unsigned int)c < 0x20 || c == 0x7f;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
int isdigit(int c)
|
||||
{
|
||||
return (unsigned int)(c - '0') < 10;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
int isgraph(int c)
|
||||
{
|
||||
/* 0x21..0x7e */
|
||||
return (unsigned int)(c - 0x21) < 0x5e;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
int islower(int c)
|
||||
{
|
||||
return (unsigned int)(c - 'a') < 26;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
int isprint(int c)
|
||||
{
|
||||
/* 0x20..0x7e */
|
||||
return (unsigned int)(c - 0x20) < 0x5f;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
int isspace(int c)
|
||||
{
|
||||
/* \t is 0x9, \n is 0xA, \v is 0xB, \f is 0xC, \r is 0xD */
|
||||
return ((unsigned int)c == ' ') || (unsigned int)(c - 0x09) < 5;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
int isupper(int c)
|
||||
{
|
||||
return (unsigned int)(c - 'A') < 26;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
int isxdigit(int c)
|
||||
{
|
||||
return isdigit(c) || (unsigned int)(c - 'A') < 6 || (unsigned int)(c - 'a') < 6;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
int isalpha(int c)
|
||||
{
|
||||
return islower(c) || isupper(c);
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
int isalnum(int c)
|
||||
{
|
||||
return isalpha(c) || isdigit(c);
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
int ispunct(int c)
|
||||
{
|
||||
return isgraph(c) && !isalnum(c);
|
||||
}
|
||||
|
||||
/* make sure to include all global symbols */
|
||||
#include "nolibc.h"
|
||||
|
||||
#endif /* _NOLIBC_CTYPE_H */
|
28
nolibc/errno.h
Normal file
28
nolibc/errno.h
Normal file
|
@ -0,0 +1,28 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
|
||||
/*
|
||||
* Minimal errno definitions for NOLIBC
|
||||
* Copyright (C) 2017-2022 Willy Tarreau <w@1wt.eu>
|
||||
*/
|
||||
|
||||
#ifndef _NOLIBC_ERRNO_H
|
||||
#define _NOLIBC_ERRNO_H
|
||||
|
||||
#include <asm/errno.h>
|
||||
|
||||
#ifndef NOLIBC_IGNORE_ERRNO
|
||||
#define SET_ERRNO(v) do { errno = (v); } while (0)
|
||||
int errno __attribute__((weak));
|
||||
#else
|
||||
#define SET_ERRNO(v) do { } while (0)
|
||||
#endif
|
||||
|
||||
|
||||
/* errno codes all ensure that they will not conflict with a valid pointer
|
||||
* because they all correspond to the highest addressable memory page.
|
||||
*/
|
||||
#define MAX_ERRNO 4095
|
||||
|
||||
/* make sure to include all global symbols */
|
||||
#include "nolibc.h"
|
||||
|
||||
#endif /* _NOLIBC_ERRNO_H */
|
112
nolibc/nolibc.h
Normal file
112
nolibc/nolibc.h
Normal file
|
@ -0,0 +1,112 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
|
||||
/* nolibc.h
|
||||
* Copyright (C) 2017-2018 Willy Tarreau <w@1wt.eu>
|
||||
*/
|
||||
|
||||
/*
|
||||
* This file is designed to be used as a libc alternative for minimal programs
|
||||
* with very limited requirements. It consists of a small number of syscall and
|
||||
* type definitions, and the minimal startup code needed to call main().
|
||||
* All syscalls are declared as static functions so that they can be optimized
|
||||
* away by the compiler when not used.
|
||||
*
|
||||
* Syscalls are split into 3 levels:
|
||||
* - The lower level is the arch-specific syscall() definition, consisting in
|
||||
* assembly code in compound expressions. These are called my_syscall0() to
|
||||
* my_syscall6() depending on the number of arguments. The MIPS
|
||||
* implementation is limited to 5 arguments. All input arguments are cast
|
||||
* to a long stored in a register. These expressions always return the
|
||||
* syscall's return value as a signed long value which is often either a
|
||||
* pointer or the negated errno value.
|
||||
*
|
||||
* - The second level is mostly architecture-independent. It is made of
|
||||
* static functions called sys_<name>() which rely on my_syscallN()
|
||||
* depending on the syscall definition. These functions are responsible
|
||||
* for exposing the appropriate types for the syscall arguments (int,
|
||||
* pointers, etc) and for setting the appropriate return type (often int).
|
||||
* A few of them are architecture-specific because the syscalls are not all
|
||||
* mapped exactly the same among architectures. For example, some archs do
|
||||
* not implement select() and need pselect6() instead, so the sys_select()
|
||||
* function will have to abstract this.
|
||||
*
|
||||
* - The third level is the libc call definition. It exposes the lower raw
|
||||
* sys_<name>() calls in a way that looks like what a libc usually does,
|
||||
* takes care of specific input values, and of setting errno upon error.
|
||||
* There can be minor variations compared to standard libc calls. For
|
||||
* example the open() call always takes 3 args here.
|
||||
*
|
||||
* The errno variable is declared static and unused. This way it can be
|
||||
* optimized away if not used. However this means that a program made of
|
||||
* multiple C files may observe different errno values (one per C file). For
|
||||
* the type of programs this project targets it usually is not a problem. The
|
||||
* resulting program may even be reduced by defining the NOLIBC_IGNORE_ERRNO
|
||||
* macro, in which case the errno value will never be assigned.
|
||||
*
|
||||
* Some stdint-like integer types are defined. These are valid on all currently
|
||||
* supported architectures, because signs are enforced, ints are assumed to be
|
||||
* 32 bits, longs the size of a pointer and long long 64 bits. If more
|
||||
* architectures have to be supported, this may need to be adapted.
|
||||
*
|
||||
* Some macro definitions like the O_* values passed to open(), and some
|
||||
* structures like the sys_stat struct depend on the architecture.
|
||||
*
|
||||
* The definitions start with the architecture-specific parts, which are picked
|
||||
* based on what the compiler knows about the target architecture, and are
|
||||
* completed with the generic code. Since it is the compiler which sets the
|
||||
* target architecture, cross-compiling normally works out of the box without
|
||||
* having to specify anything.
|
||||
*
|
||||
* Finally some very common libc-level functions are provided. It is the case
|
||||
* for a few functions usually found in string.h, ctype.h, or stdlib.h.
|
||||
*
|
||||
* The nolibc.h file is only a convenient entry point which includes all other
|
||||
* files. It also defines the NOLIBC macro, so that it is possible for a
|
||||
* program to check this macro to know if it is being built against and decide
|
||||
* to disable some features or simply not to include some standard libc files.
|
||||
*
|
||||
* A simple static executable may be built this way :
|
||||
* $ gcc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \
|
||||
* -static -include nolibc.h -o hello hello.c -lgcc
|
||||
*
|
||||
* Simple programs meant to be reasonably portable to various libc and using
|
||||
* only a few common includes, may also be built by simply making the include
|
||||
* path point to the nolibc directory:
|
||||
* $ gcc -fno-asynchronous-unwind-tables -fno-ident -s -Os -nostdlib \
|
||||
* -I../nolibc -o hello hello.c -lgcc
|
||||
*
|
||||
* The available standard (but limited) include files are:
|
||||
* ctype.h, errno.h, signal.h, stdio.h, stdlib.h, string.h, time.h
|
||||
*
|
||||
* In addition, the following ones are expected to be provided by the compiler:
|
||||
* float.h, stdarg.h, stddef.h
|
||||
*
|
||||
* The following ones which are part to the C standard are not provided:
|
||||
* assert.h, locale.h, math.h, setjmp.h, limits.h
|
||||
*
|
||||
* A very useful calling convention table may be found here :
|
||||
* http://man7.org/linux/man-pages/man2/syscall.2.html
|
||||
*
|
||||
* This doc is quite convenient though not necessarily up to date :
|
||||
* https://w3challs.com/syscalls/
|
||||
*
|
||||
*/
|
||||
#ifndef _NOLIBC_H
|
||||
#define _NOLIBC_H
|
||||
|
||||
#include "std.h"
|
||||
#include "arch.h"
|
||||
#include "types.h"
|
||||
#include "sys.h"
|
||||
#include "ctype.h"
|
||||
#include "signal.h"
|
||||
#include "unistd.h"
|
||||
#include "stdio.h"
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
#include "time.h"
|
||||
#include "stackprotector.h"
|
||||
|
||||
/* Used by programs to avoid std includes */
|
||||
#define NOLIBC
|
||||
|
||||
#endif /* _NOLIBC_H */
|
25
nolibc/signal.h
Normal file
25
nolibc/signal.h
Normal file
|
@ -0,0 +1,25 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
|
||||
/*
|
||||
* signal function definitions for NOLIBC
|
||||
* Copyright (C) 2017-2022 Willy Tarreau <w@1wt.eu>
|
||||
*/
|
||||
|
||||
#ifndef _NOLIBC_SIGNAL_H
|
||||
#define _NOLIBC_SIGNAL_H
|
||||
|
||||
#include "std.h"
|
||||
#include "arch.h"
|
||||
#include "types.h"
|
||||
#include "sys.h"
|
||||
|
||||
/* This one is not marked static as it's needed by libgcc for divide by zero */
|
||||
__attribute__((weak,unused,section(".text.nolibc_raise")))
|
||||
int raise(int signal)
|
||||
{
|
||||
return sys_kill(sys_getpid(), signal);
|
||||
}
|
||||
|
||||
/* make sure to include all global symbols */
|
||||
#include "nolibc.h"
|
||||
|
||||
#endif /* _NOLIBC_SIGNAL_H */
|
50
nolibc/stackprotector.h
Normal file
50
nolibc/stackprotector.h
Normal file
|
@ -0,0 +1,50 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
|
||||
/*
|
||||
* Stack protector support for NOLIBC
|
||||
* Copyright (C) 2023 Thomas Weißschuh <linux@weissschuh.net>
|
||||
*/
|
||||
|
||||
#ifndef _NOLIBC_STACKPROTECTOR_H
|
||||
#define _NOLIBC_STACKPROTECTOR_H
|
||||
|
||||
#include "compiler.h"
|
||||
|
||||
#if defined(_NOLIBC_STACKPROTECTOR)
|
||||
|
||||
#include "sys.h"
|
||||
#include "stdlib.h"
|
||||
|
||||
/* The functions in this header are using raw syscall macros to avoid
|
||||
* triggering stack protector errors themselves
|
||||
*/
|
||||
|
||||
__attribute__((weak,noreturn,section(".text.nolibc_stack_chk")))
|
||||
void __stack_chk_fail(void)
|
||||
{
|
||||
pid_t pid;
|
||||
my_syscall3(__NR_write, STDERR_FILENO, "!!Stack smashing detected!!\n", 28);
|
||||
pid = my_syscall0(__NR_getpid);
|
||||
my_syscall2(__NR_kill, pid, SIGABRT);
|
||||
for (;;);
|
||||
}
|
||||
|
||||
__attribute__((weak,noreturn,section(".text.nolibc_stack_chk")))
|
||||
void __stack_chk_fail_local(void)
|
||||
{
|
||||
__stack_chk_fail();
|
||||
}
|
||||
|
||||
__attribute__((weak,section(".data.nolibc_stack_chk")))
|
||||
uintptr_t __stack_chk_guard;
|
||||
|
||||
__attribute__((weak,section(".text.nolibc_stack_chk"))) __no_stack_protector
|
||||
void __stack_chk_init(void)
|
||||
{
|
||||
my_syscall3(__NR_getrandom, &__stack_chk_guard, sizeof(__stack_chk_guard), 0);
|
||||
/* a bit more randomness in case getrandom() fails, ensure the guard is never 0 */
|
||||
if (__stack_chk_guard != (uintptr_t) &__stack_chk_guard)
|
||||
__stack_chk_guard ^= (uintptr_t) &__stack_chk_guard;
|
||||
}
|
||||
#endif /* defined(_NOLIBC_STACKPROTECTOR) */
|
||||
|
||||
#endif /* _NOLIBC_STACKPROTECTOR_H */
|
36
nolibc/std.h
Normal file
36
nolibc/std.h
Normal file
|
@ -0,0 +1,36 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
|
||||
/*
|
||||
* Standard definitions and types for NOLIBC
|
||||
* Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
|
||||
*/
|
||||
|
||||
#ifndef _NOLIBC_STD_H
|
||||
#define _NOLIBC_STD_H
|
||||
|
||||
/* Declare a few quite common macros and types that usually are in stdlib.h,
|
||||
* stdint.h, ctype.h, unistd.h and a few other common locations. Please place
|
||||
* integer type definitions and generic macros here, but avoid OS-specific and
|
||||
* syscall-specific stuff, as this file is expected to be included very early.
|
||||
*/
|
||||
|
||||
/* note: may already be defined */
|
||||
#ifndef NULL
|
||||
#define NULL ((void *)0)
|
||||
#endif
|
||||
|
||||
#include "stdint.h"
|
||||
|
||||
/* those are commonly provided by sys/types.h */
|
||||
typedef unsigned int dev_t;
|
||||
typedef unsigned long ino_t;
|
||||
typedef unsigned int mode_t;
|
||||
typedef signed int pid_t;
|
||||
typedef unsigned int uid_t;
|
||||
typedef unsigned int gid_t;
|
||||
typedef unsigned long nlink_t;
|
||||
typedef signed long off_t;
|
||||
typedef signed long blksize_t;
|
||||
typedef signed long blkcnt_t;
|
||||
typedef signed long time_t;
|
||||
|
||||
#endif /* _NOLIBC_STD_H */
|
113
nolibc/stdint.h
Normal file
113
nolibc/stdint.h
Normal file
|
@ -0,0 +1,113 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
|
||||
/*
|
||||
* Standard definitions and types for NOLIBC
|
||||
* Copyright (C) 2023 Vincent Dagonneau <v@vda.io>
|
||||
*/
|
||||
|
||||
#ifndef _NOLIBC_STDINT_H
|
||||
#define _NOLIBC_STDINT_H
|
||||
|
||||
typedef unsigned char uint8_t;
|
||||
typedef signed char int8_t;
|
||||
typedef unsigned short uint16_t;
|
||||
typedef signed short int16_t;
|
||||
typedef unsigned int uint32_t;
|
||||
typedef signed int int32_t;
|
||||
typedef unsigned long long uint64_t;
|
||||
typedef signed long long int64_t;
|
||||
typedef unsigned long size_t;
|
||||
typedef signed long ssize_t;
|
||||
typedef unsigned long uintptr_t;
|
||||
typedef signed long intptr_t;
|
||||
typedef signed long ptrdiff_t;
|
||||
|
||||
typedef int8_t int_least8_t;
|
||||
typedef uint8_t uint_least8_t;
|
||||
typedef int16_t int_least16_t;
|
||||
typedef uint16_t uint_least16_t;
|
||||
typedef int32_t int_least32_t;
|
||||
typedef uint32_t uint_least32_t;
|
||||
typedef int64_t int_least64_t;
|
||||
typedef uint64_t uint_least64_t;
|
||||
|
||||
typedef int8_t int_fast8_t;
|
||||
typedef uint8_t uint_fast8_t;
|
||||
typedef ssize_t int_fast16_t;
|
||||
typedef size_t uint_fast16_t;
|
||||
typedef ssize_t int_fast32_t;
|
||||
typedef size_t uint_fast32_t;
|
||||
typedef int64_t int_fast64_t;
|
||||
typedef uint64_t uint_fast64_t;
|
||||
|
||||
typedef int64_t intmax_t;
|
||||
typedef uint64_t uintmax_t;
|
||||
|
||||
/* limits of integral types */
|
||||
|
||||
#define INT8_MIN (-128)
|
||||
#define INT16_MIN (-32767-1)
|
||||
#define INT32_MIN (-2147483647-1)
|
||||
#define INT64_MIN (-9223372036854775807LL-1)
|
||||
|
||||
#define INT8_MAX (127)
|
||||
#define INT16_MAX (32767)
|
||||
#define INT32_MAX (2147483647)
|
||||
#define INT64_MAX (9223372036854775807LL)
|
||||
|
||||
#define UINT8_MAX (255)
|
||||
#define UINT16_MAX (65535)
|
||||
#define UINT32_MAX (4294967295U)
|
||||
#define UINT64_MAX (18446744073709551615ULL)
|
||||
|
||||
#define INT_LEAST8_MIN INT8_MIN
|
||||
#define INT_LEAST16_MIN INT16_MIN
|
||||
#define INT_LEAST32_MIN INT32_MIN
|
||||
#define INT_LEAST64_MIN INT64_MIN
|
||||
|
||||
#define INT_LEAST8_MAX INT8_MAX
|
||||
#define INT_LEAST16_MAX INT16_MAX
|
||||
#define INT_LEAST32_MAX INT32_MAX
|
||||
#define INT_LEAST64_MAX INT64_MAX
|
||||
|
||||
#define UINT_LEAST8_MAX UINT8_MAX
|
||||
#define UINT_LEAST16_MAX UINT16_MAX
|
||||
#define UINT_LEAST32_MAX UINT32_MAX
|
||||
#define UINT_LEAST64_MAX UINT64_MAX
|
||||
|
||||
#define SIZE_MAX ((size_t)(__LONG_MAX__) * 2 + 1)
|
||||
#define INTPTR_MIN (-__LONG_MAX__ - 1)
|
||||
#define INTPTR_MAX __LONG_MAX__
|
||||
#define PTRDIFF_MIN INTPTR_MIN
|
||||
#define PTRDIFF_MAX INTPTR_MAX
|
||||
#define UINTPTR_MAX SIZE_MAX
|
||||
|
||||
#define INT_FAST8_MIN INT8_MIN
|
||||
#define INT_FAST16_MIN INTPTR_MIN
|
||||
#define INT_FAST32_MIN INTPTR_MIN
|
||||
#define INT_FAST64_MIN INT64_MIN
|
||||
|
||||
#define INT_FAST8_MAX INT8_MAX
|
||||
#define INT_FAST16_MAX INTPTR_MAX
|
||||
#define INT_FAST32_MAX INTPTR_MAX
|
||||
#define INT_FAST64_MAX INT64_MAX
|
||||
|
||||
#define UINT_FAST8_MAX UINT8_MAX
|
||||
#define UINT_FAST16_MAX SIZE_MAX
|
||||
#define UINT_FAST32_MAX SIZE_MAX
|
||||
#define UINT_FAST64_MAX UINT64_MAX
|
||||
|
||||
#ifndef INT_MIN
|
||||
#define INT_MIN (-__INT_MAX__ - 1)
|
||||
#endif
|
||||
#ifndef INT_MAX
|
||||
#define INT_MAX __INT_MAX__
|
||||
#endif
|
||||
|
||||
#ifndef LONG_MIN
|
||||
#define LONG_MIN (-__LONG_MAX__ - 1)
|
||||
#endif
|
||||
#ifndef LONG_MAX
|
||||
#define LONG_MAX __LONG_MAX__
|
||||
#endif
|
||||
|
||||
#endif /* _NOLIBC_STDINT_H */
|
356
nolibc/stdio.h
Normal file
356
nolibc/stdio.h
Normal file
|
@ -0,0 +1,356 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
|
||||
/*
|
||||
* minimal stdio function definitions for NOLIBC
|
||||
* Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
|
||||
*/
|
||||
|
||||
#ifndef _NOLIBC_STDIO_H
|
||||
#define _NOLIBC_STDIO_H
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
#include "std.h"
|
||||
#include "arch.h"
|
||||
#include "errno.h"
|
||||
#include "types.h"
|
||||
#include "sys.h"
|
||||
#include "stdlib.h"
|
||||
#include "string.h"
|
||||
|
||||
#ifndef EOF
|
||||
#define EOF (-1)
|
||||
#endif
|
||||
|
||||
/* just define FILE as a non-empty type. The value of the pointer gives
|
||||
* the FD: FILE=~fd for fd>=0 or NULL for fd<0. This way positive FILE
|
||||
* are immediately identified as abnormal entries (i.e. possible copies
|
||||
* of valid pointers to something else).
|
||||
*/
|
||||
typedef struct FILE {
|
||||
char dummy[1];
|
||||
} FILE;
|
||||
|
||||
static __attribute__((unused)) FILE* const stdin = (FILE*)(intptr_t)~STDIN_FILENO;
|
||||
static __attribute__((unused)) FILE* const stdout = (FILE*)(intptr_t)~STDOUT_FILENO;
|
||||
static __attribute__((unused)) FILE* const stderr = (FILE*)(intptr_t)~STDERR_FILENO;
|
||||
|
||||
/* provides a FILE* equivalent of fd. The mode is ignored. */
|
||||
static __attribute__((unused))
|
||||
FILE *fdopen(int fd, const char *mode __attribute__((unused)))
|
||||
{
|
||||
if (fd < 0) {
|
||||
SET_ERRNO(EBADF);
|
||||
return NULL;
|
||||
}
|
||||
return (FILE*)(intptr_t)~fd;
|
||||
}
|
||||
|
||||
/* provides the fd of stream. */
|
||||
static __attribute__((unused))
|
||||
int fileno(FILE *stream)
|
||||
{
|
||||
intptr_t i = (intptr_t)stream;
|
||||
|
||||
if (i >= 0) {
|
||||
SET_ERRNO(EBADF);
|
||||
return -1;
|
||||
}
|
||||
return ~i;
|
||||
}
|
||||
|
||||
/* flush a stream. */
|
||||
static __attribute__((unused))
|
||||
int fflush(FILE *stream)
|
||||
{
|
||||
intptr_t i = (intptr_t)stream;
|
||||
|
||||
/* NULL is valid here. */
|
||||
if (i > 0) {
|
||||
SET_ERRNO(EBADF);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* Don't do anything, nolibc does not support buffering. */
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* flush a stream. */
|
||||
static __attribute__((unused))
|
||||
int fclose(FILE *stream)
|
||||
{
|
||||
intptr_t i = (intptr_t)stream;
|
||||
|
||||
if (i >= 0) {
|
||||
SET_ERRNO(EBADF);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (close(~i))
|
||||
return EOF;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* getc(), fgetc(), getchar() */
|
||||
|
||||
#define getc(stream) fgetc(stream)
|
||||
|
||||
static __attribute__((unused))
|
||||
int fgetc(FILE* stream)
|
||||
{
|
||||
unsigned char ch;
|
||||
|
||||
if (read(fileno(stream), &ch, 1) <= 0)
|
||||
return EOF;
|
||||
return ch;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
int getchar(void)
|
||||
{
|
||||
return fgetc(stdin);
|
||||
}
|
||||
|
||||
|
||||
/* putc(), fputc(), putchar() */
|
||||
|
||||
#define putc(c, stream) fputc(c, stream)
|
||||
|
||||
static __attribute__((unused))
|
||||
int fputc(int c, FILE* stream)
|
||||
{
|
||||
unsigned char ch = c;
|
||||
|
||||
if (write(fileno(stream), &ch, 1) <= 0)
|
||||
return EOF;
|
||||
return ch;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
int putchar(int c)
|
||||
{
|
||||
return fputc(c, stdout);
|
||||
}
|
||||
|
||||
|
||||
/* fwrite(), puts(), fputs(). Note that puts() emits '\n' but not fputs(). */
|
||||
|
||||
/* internal fwrite()-like function which only takes a size and returns 0 on
|
||||
* success or EOF on error. It automatically retries on short writes.
|
||||
*/
|
||||
static __attribute__((unused))
|
||||
int _fwrite(const void *buf, size_t size, FILE *stream)
|
||||
{
|
||||
ssize_t ret;
|
||||
int fd = fileno(stream);
|
||||
|
||||
while (size) {
|
||||
ret = write(fd, buf, size);
|
||||
if (ret <= 0)
|
||||
return EOF;
|
||||
size -= ret;
|
||||
buf += ret;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
size_t fwrite(const void *s, size_t size, size_t nmemb, FILE *stream)
|
||||
{
|
||||
size_t written;
|
||||
|
||||
for (written = 0; written < nmemb; written++) {
|
||||
if (_fwrite(s, size, stream) != 0)
|
||||
break;
|
||||
s += size;
|
||||
}
|
||||
return written;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
int fputs(const char *s, FILE *stream)
|
||||
{
|
||||
return _fwrite(s, strlen(s), stream);
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
int puts(const char *s)
|
||||
{
|
||||
if (fputs(s, stdout) == EOF)
|
||||
return EOF;
|
||||
return putchar('\n');
|
||||
}
|
||||
|
||||
|
||||
/* fgets() */
|
||||
static __attribute__((unused))
|
||||
char *fgets(char *s, int size, FILE *stream)
|
||||
{
|
||||
int ofs;
|
||||
int c;
|
||||
|
||||
for (ofs = 0; ofs + 1 < size;) {
|
||||
c = fgetc(stream);
|
||||
if (c == EOF)
|
||||
break;
|
||||
s[ofs++] = c;
|
||||
if (c == '\n')
|
||||
break;
|
||||
}
|
||||
if (ofs < size)
|
||||
s[ofs] = 0;
|
||||
return ofs ? s : NULL;
|
||||
}
|
||||
|
||||
|
||||
/* minimal vfprintf(). It supports the following formats:
|
||||
* - %[l*]{d,u,c,x,p}
|
||||
* - %s
|
||||
* - unknown modifiers are ignored.
|
||||
*/
|
||||
static __attribute__((unused))
|
||||
int vfprintf(FILE *stream, const char *fmt, va_list args)
|
||||
{
|
||||
char escape, lpref, c;
|
||||
unsigned long long v;
|
||||
unsigned int written;
|
||||
size_t len, ofs;
|
||||
char tmpbuf[21];
|
||||
const char *outstr;
|
||||
|
||||
written = ofs = escape = lpref = 0;
|
||||
while (1) {
|
||||
c = fmt[ofs++];
|
||||
|
||||
if (escape) {
|
||||
/* we're in an escape sequence, ofs == 1 */
|
||||
escape = 0;
|
||||
if (c == 'c' || c == 'd' || c == 'u' || c == 'x' || c == 'p') {
|
||||
char *out = tmpbuf;
|
||||
|
||||
if (c == 'p')
|
||||
v = va_arg(args, unsigned long);
|
||||
else if (lpref) {
|
||||
if (lpref > 1)
|
||||
v = va_arg(args, unsigned long long);
|
||||
else
|
||||
v = va_arg(args, unsigned long);
|
||||
} else
|
||||
v = va_arg(args, unsigned int);
|
||||
|
||||
if (c == 'd') {
|
||||
/* sign-extend the value */
|
||||
if (lpref == 0)
|
||||
v = (long long)(int)v;
|
||||
else if (lpref == 1)
|
||||
v = (long long)(long)v;
|
||||
}
|
||||
|
||||
switch (c) {
|
||||
case 'c':
|
||||
out[0] = v;
|
||||
out[1] = 0;
|
||||
break;
|
||||
case 'd':
|
||||
i64toa_r(v, out);
|
||||
break;
|
||||
case 'u':
|
||||
u64toa_r(v, out);
|
||||
break;
|
||||
case 'p':
|
||||
*(out++) = '0';
|
||||
*(out++) = 'x';
|
||||
/* fall through */
|
||||
default: /* 'x' and 'p' above */
|
||||
u64toh_r(v, out);
|
||||
break;
|
||||
}
|
||||
outstr = tmpbuf;
|
||||
}
|
||||
else if (c == 's') {
|
||||
outstr = va_arg(args, char *);
|
||||
if (!outstr)
|
||||
outstr="(null)";
|
||||
}
|
||||
else if (c == '%') {
|
||||
/* queue it verbatim */
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
/* modifiers or final 0 */
|
||||
if (c == 'l') {
|
||||
/* long format prefix, maintain the escape */
|
||||
lpref++;
|
||||
}
|
||||
escape = 1;
|
||||
goto do_escape;
|
||||
}
|
||||
len = strlen(outstr);
|
||||
goto flush_str;
|
||||
}
|
||||
|
||||
/* not an escape sequence */
|
||||
if (c == 0 || c == '%') {
|
||||
/* flush pending data on escape or end */
|
||||
escape = 1;
|
||||
lpref = 0;
|
||||
outstr = fmt;
|
||||
len = ofs - 1;
|
||||
flush_str:
|
||||
if (_fwrite(outstr, len, stream) != 0)
|
||||
break;
|
||||
|
||||
written += len;
|
||||
do_escape:
|
||||
if (c == 0)
|
||||
break;
|
||||
fmt += ofs;
|
||||
ofs = 0;
|
||||
continue;
|
||||
}
|
||||
|
||||
/* literal char, just queue it */
|
||||
}
|
||||
return written;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
int vprintf(const char *fmt, va_list args)
|
||||
{
|
||||
return vfprintf(stdout, fmt, args);
|
||||
}
|
||||
|
||||
static __attribute__((unused, format(printf, 2, 3)))
|
||||
int fprintf(FILE *stream, const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
int ret;
|
||||
|
||||
va_start(args, fmt);
|
||||
ret = vfprintf(stream, fmt, args);
|
||||
va_end(args);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static __attribute__((unused, format(printf, 1, 2)))
|
||||
int printf(const char *fmt, ...)
|
||||
{
|
||||
va_list args;
|
||||
int ret;
|
||||
|
||||
va_start(args, fmt);
|
||||
ret = vfprintf(stdout, fmt, args);
|
||||
va_end(args);
|
||||
return ret;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
void perror(const char *msg)
|
||||
{
|
||||
fprintf(stderr, "%s%serrno=%d\n", (msg && *msg) ? msg : "", (msg && *msg) ? ": " : "", errno);
|
||||
}
|
||||
|
||||
/* make sure to include all global symbols */
|
||||
#include "nolibc.h"
|
||||
|
||||
#endif /* _NOLIBC_STDIO_H */
|
452
nolibc/stdlib.h
Normal file
452
nolibc/stdlib.h
Normal file
|
@ -0,0 +1,452 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
|
||||
/*
|
||||
* stdlib function definitions for NOLIBC
|
||||
* Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
|
||||
*/
|
||||
|
||||
#ifndef _NOLIBC_STDLIB_H
|
||||
#define _NOLIBC_STDLIB_H
|
||||
|
||||
#include "std.h"
|
||||
#include "arch.h"
|
||||
#include "types.h"
|
||||
#include "sys.h"
|
||||
#include "string.h"
|
||||
#include <linux/auxvec.h>
|
||||
|
||||
struct nolibc_heap {
|
||||
size_t len;
|
||||
char user_p[] __attribute__((__aligned__));
|
||||
};
|
||||
|
||||
/* Buffer used to store int-to-ASCII conversions. Will only be implemented if
|
||||
* any of the related functions is implemented. The area is large enough to
|
||||
* store "18446744073709551615" or "-9223372036854775808" and the final zero.
|
||||
*/
|
||||
static __attribute__((unused)) char itoa_buffer[21];
|
||||
|
||||
/*
|
||||
* As much as possible, please keep functions alphabetically sorted.
|
||||
*/
|
||||
|
||||
/* must be exported, as it's used by libgcc for various divide functions */
|
||||
__attribute__((weak,unused,noreturn,section(".text.nolibc_abort")))
|
||||
void abort(void)
|
||||
{
|
||||
sys_kill(sys_getpid(), SIGABRT);
|
||||
for (;;);
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
long atol(const char *s)
|
||||
{
|
||||
unsigned long ret = 0;
|
||||
unsigned long d;
|
||||
int neg = 0;
|
||||
|
||||
if (*s == '-') {
|
||||
neg = 1;
|
||||
s++;
|
||||
}
|
||||
|
||||
while (1) {
|
||||
d = (*s++) - '0';
|
||||
if (d > 9)
|
||||
break;
|
||||
ret *= 10;
|
||||
ret += d;
|
||||
}
|
||||
|
||||
return neg ? -ret : ret;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
int atoi(const char *s)
|
||||
{
|
||||
return atol(s);
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
void free(void *ptr)
|
||||
{
|
||||
struct nolibc_heap *heap;
|
||||
|
||||
if (!ptr)
|
||||
return;
|
||||
|
||||
heap = container_of(ptr, struct nolibc_heap, user_p);
|
||||
munmap(heap, heap->len);
|
||||
}
|
||||
|
||||
/* getenv() tries to find the environment variable named <name> in the
|
||||
* environment array pointed to by global variable "environ" which must be
|
||||
* declared as a char **, and must be terminated by a NULL (it is recommended
|
||||
* to set this variable to the "envp" argument of main()). If the requested
|
||||
* environment variable exists its value is returned otherwise NULL is
|
||||
* returned. getenv() is forcefully inlined so that the reference to "environ"
|
||||
* will be dropped if unused, even at -O0.
|
||||
*/
|
||||
static __attribute__((unused))
|
||||
char *_getenv(const char *name, char **environ)
|
||||
{
|
||||
int idx, i;
|
||||
|
||||
if (environ) {
|
||||
for (idx = 0; environ[idx]; idx++) {
|
||||
for (i = 0; name[i] && name[i] == environ[idx][i];)
|
||||
i++;
|
||||
if (!name[i] && environ[idx][i] == '=')
|
||||
return &environ[idx][i+1];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static __inline__ __attribute__((unused,always_inline))
|
||||
char *getenv(const char *name)
|
||||
{
|
||||
extern char **environ;
|
||||
return _getenv(name, environ);
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
unsigned long getauxval(unsigned long type)
|
||||
{
|
||||
const unsigned long *auxv = _auxv;
|
||||
unsigned long ret;
|
||||
|
||||
if (!auxv)
|
||||
return 0;
|
||||
|
||||
while (1) {
|
||||
if (!auxv[0] && !auxv[1]) {
|
||||
ret = 0;
|
||||
break;
|
||||
}
|
||||
|
||||
if (auxv[0] == type) {
|
||||
ret = auxv[1];
|
||||
break;
|
||||
}
|
||||
|
||||
auxv += 2;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
void *malloc(size_t len)
|
||||
{
|
||||
struct nolibc_heap *heap;
|
||||
|
||||
/* Always allocate memory with size multiple of 4096. */
|
||||
len = sizeof(*heap) + len;
|
||||
len = (len + 4095UL) & -4096UL;
|
||||
heap = mmap(NULL, len, PROT_READ|PROT_WRITE, MAP_ANONYMOUS|MAP_PRIVATE,
|
||||
-1, 0);
|
||||
if (__builtin_expect(heap == MAP_FAILED, 0))
|
||||
return NULL;
|
||||
|
||||
heap->len = len;
|
||||
return heap->user_p;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
void *calloc(size_t size, size_t nmemb)
|
||||
{
|
||||
size_t x = size * nmemb;
|
||||
|
||||
if (__builtin_expect(size && ((x / size) != nmemb), 0)) {
|
||||
SET_ERRNO(ENOMEM);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* No need to zero the heap, the MAP_ANONYMOUS in malloc()
|
||||
* already does it.
|
||||
*/
|
||||
return malloc(x);
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
void *realloc(void *old_ptr, size_t new_size)
|
||||
{
|
||||
struct nolibc_heap *heap;
|
||||
size_t user_p_len;
|
||||
void *ret;
|
||||
|
||||
if (!old_ptr)
|
||||
return malloc(new_size);
|
||||
|
||||
heap = container_of(old_ptr, struct nolibc_heap, user_p);
|
||||
user_p_len = heap->len - sizeof(*heap);
|
||||
/*
|
||||
* Don't realloc() if @user_p_len >= @new_size, this block of
|
||||
* memory is still enough to handle the @new_size. Just return
|
||||
* the same pointer.
|
||||
*/
|
||||
if (user_p_len >= new_size)
|
||||
return old_ptr;
|
||||
|
||||
ret = malloc(new_size);
|
||||
if (__builtin_expect(!ret, 0))
|
||||
return NULL;
|
||||
|
||||
memcpy(ret, heap->user_p, heap->len);
|
||||
munmap(heap, heap->len);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* Converts the unsigned long integer <in> to its hex representation into
|
||||
* buffer <buffer>, which must be long enough to store the number and the
|
||||
* trailing zero (17 bytes for "ffffffffffffffff" or 9 for "ffffffff"). The
|
||||
* buffer is filled from the first byte, and the number of characters emitted
|
||||
* (not counting the trailing zero) is returned. The function is constructed
|
||||
* in a way to optimize the code size and avoid any divide that could add a
|
||||
* dependency on large external functions.
|
||||
*/
|
||||
static __attribute__((unused))
|
||||
int utoh_r(unsigned long in, char *buffer)
|
||||
{
|
||||
signed char pos = (~0UL > 0xfffffffful) ? 60 : 28;
|
||||
int digits = 0;
|
||||
int dig;
|
||||
|
||||
do {
|
||||
dig = in >> pos;
|
||||
in -= (uint64_t)dig << pos;
|
||||
pos -= 4;
|
||||
if (dig || digits || pos < 0) {
|
||||
if (dig > 9)
|
||||
dig += 'a' - '0' - 10;
|
||||
buffer[digits++] = '0' + dig;
|
||||
}
|
||||
} while (pos >= 0);
|
||||
|
||||
buffer[digits] = 0;
|
||||
return digits;
|
||||
}
|
||||
|
||||
/* converts unsigned long <in> to an hex string using the static itoa_buffer
|
||||
* and returns the pointer to that string.
|
||||
*/
|
||||
static __inline__ __attribute__((unused))
|
||||
char *utoh(unsigned long in)
|
||||
{
|
||||
utoh_r(in, itoa_buffer);
|
||||
return itoa_buffer;
|
||||
}
|
||||
|
||||
/* Converts the unsigned long integer <in> to its string representation into
|
||||
* buffer <buffer>, which must be long enough to store the number and the
|
||||
* trailing zero (21 bytes for 18446744073709551615 in 64-bit, 11 for
|
||||
* 4294967295 in 32-bit). The buffer is filled from the first byte, and the
|
||||
* number of characters emitted (not counting the trailing zero) is returned.
|
||||
* The function is constructed in a way to optimize the code size and avoid
|
||||
* any divide that could add a dependency on large external functions.
|
||||
*/
|
||||
static __attribute__((unused))
|
||||
int utoa_r(unsigned long in, char *buffer)
|
||||
{
|
||||
unsigned long lim;
|
||||
int digits = 0;
|
||||
int pos = (~0UL > 0xfffffffful) ? 19 : 9;
|
||||
int dig;
|
||||
|
||||
do {
|
||||
for (dig = 0, lim = 1; dig < pos; dig++)
|
||||
lim *= 10;
|
||||
|
||||
if (digits || in >= lim || !pos) {
|
||||
for (dig = 0; in >= lim; dig++)
|
||||
in -= lim;
|
||||
buffer[digits++] = '0' + dig;
|
||||
}
|
||||
} while (pos--);
|
||||
|
||||
buffer[digits] = 0;
|
||||
return digits;
|
||||
}
|
||||
|
||||
/* Converts the signed long integer <in> to its string representation into
|
||||
* buffer <buffer>, which must be long enough to store the number and the
|
||||
* trailing zero (21 bytes for -9223372036854775808 in 64-bit, 12 for
|
||||
* -2147483648 in 32-bit). The buffer is filled from the first byte, and the
|
||||
* number of characters emitted (not counting the trailing zero) is returned.
|
||||
*/
|
||||
static __attribute__((unused))
|
||||
int itoa_r(long in, char *buffer)
|
||||
{
|
||||
char *ptr = buffer;
|
||||
int len = 0;
|
||||
|
||||
if (in < 0) {
|
||||
in = -in;
|
||||
*(ptr++) = '-';
|
||||
len++;
|
||||
}
|
||||
len += utoa_r(in, ptr);
|
||||
return len;
|
||||
}
|
||||
|
||||
/* for historical compatibility, same as above but returns the pointer to the
|
||||
* buffer.
|
||||
*/
|
||||
static __inline__ __attribute__((unused))
|
||||
char *ltoa_r(long in, char *buffer)
|
||||
{
|
||||
itoa_r(in, buffer);
|
||||
return buffer;
|
||||
}
|
||||
|
||||
/* converts long integer <in> to a string using the static itoa_buffer and
|
||||
* returns the pointer to that string.
|
||||
*/
|
||||
static __inline__ __attribute__((unused))
|
||||
char *itoa(long in)
|
||||
{
|
||||
itoa_r(in, itoa_buffer);
|
||||
return itoa_buffer;
|
||||
}
|
||||
|
||||
/* converts long integer <in> to a string using the static itoa_buffer and
|
||||
* returns the pointer to that string. Same as above, for compatibility.
|
||||
*/
|
||||
static __inline__ __attribute__((unused))
|
||||
char *ltoa(long in)
|
||||
{
|
||||
itoa_r(in, itoa_buffer);
|
||||
return itoa_buffer;
|
||||
}
|
||||
|
||||
/* converts unsigned long integer <in> to a string using the static itoa_buffer
|
||||
* and returns the pointer to that string.
|
||||
*/
|
||||
static __inline__ __attribute__((unused))
|
||||
char *utoa(unsigned long in)
|
||||
{
|
||||
utoa_r(in, itoa_buffer);
|
||||
return itoa_buffer;
|
||||
}
|
||||
|
||||
/* Converts the unsigned 64-bit integer <in> to its hex representation into
|
||||
* buffer <buffer>, which must be long enough to store the number and the
|
||||
* trailing zero (17 bytes for "ffffffffffffffff"). The buffer is filled from
|
||||
* the first byte, and the number of characters emitted (not counting the
|
||||
* trailing zero) is returned. The function is constructed in a way to optimize
|
||||
* the code size and avoid any divide that could add a dependency on large
|
||||
* external functions.
|
||||
*/
|
||||
static __attribute__((unused))
|
||||
int u64toh_r(uint64_t in, char *buffer)
|
||||
{
|
||||
signed char pos = 60;
|
||||
int digits = 0;
|
||||
int dig;
|
||||
|
||||
do {
|
||||
if (sizeof(long) >= 8) {
|
||||
dig = (in >> pos) & 0xF;
|
||||
} else {
|
||||
/* 32-bit platforms: avoid a 64-bit shift */
|
||||
uint32_t d = (pos >= 32) ? (in >> 32) : in;
|
||||
dig = (d >> (pos & 31)) & 0xF;
|
||||
}
|
||||
if (dig > 9)
|
||||
dig += 'a' - '0' - 10;
|
||||
pos -= 4;
|
||||
if (dig || digits || pos < 0)
|
||||
buffer[digits++] = '0' + dig;
|
||||
} while (pos >= 0);
|
||||
|
||||
buffer[digits] = 0;
|
||||
return digits;
|
||||
}
|
||||
|
||||
/* converts uint64_t <in> to an hex string using the static itoa_buffer and
|
||||
* returns the pointer to that string.
|
||||
*/
|
||||
static __inline__ __attribute__((unused))
|
||||
char *u64toh(uint64_t in)
|
||||
{
|
||||
u64toh_r(in, itoa_buffer);
|
||||
return itoa_buffer;
|
||||
}
|
||||
|
||||
/* Converts the unsigned 64-bit integer <in> to its string representation into
|
||||
* buffer <buffer>, which must be long enough to store the number and the
|
||||
* trailing zero (21 bytes for 18446744073709551615). The buffer is filled from
|
||||
* the first byte, and the number of characters emitted (not counting the
|
||||
* trailing zero) is returned. The function is constructed in a way to optimize
|
||||
* the code size and avoid any divide that could add a dependency on large
|
||||
* external functions.
|
||||
*/
|
||||
static __attribute__((unused))
|
||||
int u64toa_r(uint64_t in, char *buffer)
|
||||
{
|
||||
unsigned long long lim;
|
||||
int digits = 0;
|
||||
int pos = 19; /* start with the highest possible digit */
|
||||
int dig;
|
||||
|
||||
do {
|
||||
for (dig = 0, lim = 1; dig < pos; dig++)
|
||||
lim *= 10;
|
||||
|
||||
if (digits || in >= lim || !pos) {
|
||||
for (dig = 0; in >= lim; dig++)
|
||||
in -= lim;
|
||||
buffer[digits++] = '0' + dig;
|
||||
}
|
||||
} while (pos--);
|
||||
|
||||
buffer[digits] = 0;
|
||||
return digits;
|
||||
}
|
||||
|
||||
/* Converts the signed 64-bit integer <in> to its string representation into
|
||||
* buffer <buffer>, which must be long enough to store the number and the
|
||||
* trailing zero (21 bytes for -9223372036854775808). The buffer is filled from
|
||||
* the first byte, and the number of characters emitted (not counting the
|
||||
* trailing zero) is returned.
|
||||
*/
|
||||
static __attribute__((unused))
|
||||
int i64toa_r(int64_t in, char *buffer)
|
||||
{
|
||||
char *ptr = buffer;
|
||||
int len = 0;
|
||||
|
||||
if (in < 0) {
|
||||
in = -in;
|
||||
*(ptr++) = '-';
|
||||
len++;
|
||||
}
|
||||
len += u64toa_r(in, ptr);
|
||||
return len;
|
||||
}
|
||||
|
||||
/* converts int64_t <in> to a string using the static itoa_buffer and returns
|
||||
* the pointer to that string.
|
||||
*/
|
||||
static __inline__ __attribute__((unused))
|
||||
char *i64toa(int64_t in)
|
||||
{
|
||||
i64toa_r(in, itoa_buffer);
|
||||
return itoa_buffer;
|
||||
}
|
||||
|
||||
/* converts uint64_t <in> to a string using the static itoa_buffer and returns
|
||||
* the pointer to that string.
|
||||
*/
|
||||
static __inline__ __attribute__((unused))
|
||||
char *u64toa(uint64_t in)
|
||||
{
|
||||
u64toa_r(in, itoa_buffer);
|
||||
return itoa_buffer;
|
||||
}
|
||||
|
||||
/* make sure to include all global symbols */
|
||||
#include "nolibc.h"
|
||||
|
||||
#endif /* _NOLIBC_STDLIB_H */
|
294
nolibc/string.h
Normal file
294
nolibc/string.h
Normal file
|
@ -0,0 +1,294 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
|
||||
/*
|
||||
* string function definitions for NOLIBC
|
||||
* Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
|
||||
*/
|
||||
|
||||
#ifndef _NOLIBC_STRING_H
|
||||
#define _NOLIBC_STRING_H
|
||||
|
||||
#include "std.h"
|
||||
|
||||
static void *malloc(size_t len);
|
||||
|
||||
/*
|
||||
* As much as possible, please keep functions alphabetically sorted.
|
||||
*/
|
||||
|
||||
static __attribute__((unused))
|
||||
int memcmp(const void *s1, const void *s2, size_t n)
|
||||
{
|
||||
size_t ofs = 0;
|
||||
int c1 = 0;
|
||||
|
||||
while (ofs < n && !(c1 = ((unsigned char *)s1)[ofs] - ((unsigned char *)s2)[ofs])) {
|
||||
ofs++;
|
||||
}
|
||||
return c1;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
void *_nolibc_memcpy_up(void *dst, const void *src, size_t len)
|
||||
{
|
||||
size_t pos = 0;
|
||||
|
||||
while (pos < len) {
|
||||
((char *)dst)[pos] = ((const char *)src)[pos];
|
||||
pos++;
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
void *_nolibc_memcpy_down(void *dst, const void *src, size_t len)
|
||||
{
|
||||
while (len) {
|
||||
len--;
|
||||
((char *)dst)[len] = ((const char *)src)[len];
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
/* might be ignored by the compiler without -ffreestanding, then found as
|
||||
* missing.
|
||||
*/
|
||||
__attribute__((weak,unused,section(".text.nolibc_memmove")))
|
||||
void *memmove(void *dst, const void *src, size_t len)
|
||||
{
|
||||
size_t dir, pos;
|
||||
|
||||
pos = len;
|
||||
dir = -1;
|
||||
|
||||
if (dst < src) {
|
||||
pos = -1;
|
||||
dir = 1;
|
||||
}
|
||||
|
||||
while (len) {
|
||||
pos += dir;
|
||||
((char *)dst)[pos] = ((const char *)src)[pos];
|
||||
len--;
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
/* must be exported, as it's used by libgcc on ARM */
|
||||
__attribute__((weak,unused,section(".text.nolibc_memcpy")))
|
||||
void *memcpy(void *dst, const void *src, size_t len)
|
||||
{
|
||||
return _nolibc_memcpy_up(dst, src, len);
|
||||
}
|
||||
|
||||
/* might be ignored by the compiler without -ffreestanding, then found as
|
||||
* missing.
|
||||
*/
|
||||
__attribute__((weak,unused,section(".text.nolibc_memset")))
|
||||
void *memset(void *dst, int b, size_t len)
|
||||
{
|
||||
char *p = dst;
|
||||
|
||||
while (len--) {
|
||||
/* prevent gcc from recognizing memset() here */
|
||||
__asm__ volatile("");
|
||||
*(p++) = b;
|
||||
}
|
||||
return dst;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
char *strchr(const char *s, int c)
|
||||
{
|
||||
while (*s) {
|
||||
if (*s == (char)c)
|
||||
return (char *)s;
|
||||
s++;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
int strcmp(const char *a, const char *b)
|
||||
{
|
||||
unsigned int c;
|
||||
int diff;
|
||||
|
||||
while (!(diff = (unsigned char)*a++ - (c = (unsigned char)*b++)) && c)
|
||||
;
|
||||
return diff;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
char *strcpy(char *dst, const char *src)
|
||||
{
|
||||
char *ret = dst;
|
||||
|
||||
while ((*dst++ = *src++));
|
||||
return ret;
|
||||
}
|
||||
|
||||
/* this function is only used with arguments that are not constants or when
|
||||
* it's not known because optimizations are disabled. Note that gcc 12
|
||||
* recognizes an strlen() pattern and replaces it with a jump to strlen(),
|
||||
* thus itself, hence the asm() statement below that's meant to disable this
|
||||
* confusing practice.
|
||||
*/
|
||||
static __attribute__((unused))
|
||||
size_t strlen(const char *str)
|
||||
{
|
||||
size_t len;
|
||||
|
||||
for (len = 0; str[len]; len++)
|
||||
__asm__("");
|
||||
return len;
|
||||
}
|
||||
|
||||
/* do not trust __builtin_constant_p() at -O0, as clang will emit a test and
|
||||
* the two branches, then will rely on an external definition of strlen().
|
||||
*/
|
||||
#if defined(__OPTIMIZE__)
|
||||
#define nolibc_strlen(x) strlen(x)
|
||||
#define strlen(str) ({ \
|
||||
__builtin_constant_p((str)) ? \
|
||||
__builtin_strlen((str)) : \
|
||||
nolibc_strlen((str)); \
|
||||
})
|
||||
#endif
|
||||
|
||||
static __attribute__((unused))
|
||||
size_t strnlen(const char *str, size_t maxlen)
|
||||
{
|
||||
size_t len;
|
||||
|
||||
for (len = 0; (len < maxlen) && str[len]; len++);
|
||||
return len;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
char *strdup(const char *str)
|
||||
{
|
||||
size_t len;
|
||||
char *ret;
|
||||
|
||||
len = strlen(str);
|
||||
ret = malloc(len + 1);
|
||||
if (__builtin_expect(ret != NULL, 1))
|
||||
memcpy(ret, str, len + 1);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
char *strndup(const char *str, size_t maxlen)
|
||||
{
|
||||
size_t len;
|
||||
char *ret;
|
||||
|
||||
len = strnlen(str, maxlen);
|
||||
ret = malloc(len + 1);
|
||||
if (__builtin_expect(ret != NULL, 1)) {
|
||||
memcpy(ret, str, len);
|
||||
ret[len] = '\0';
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
size_t strlcat(char *dst, const char *src, size_t size)
|
||||
{
|
||||
size_t len;
|
||||
char c;
|
||||
|
||||
for (len = 0; dst[len]; len++)
|
||||
;
|
||||
|
||||
for (;;) {
|
||||
c = *src;
|
||||
if (len < size)
|
||||
dst[len] = c;
|
||||
if (!c)
|
||||
break;
|
||||
len++;
|
||||
src++;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
size_t strlcpy(char *dst, const char *src, size_t size)
|
||||
{
|
||||
size_t len;
|
||||
char c;
|
||||
|
||||
for (len = 0;;) {
|
||||
c = src[len];
|
||||
if (len < size)
|
||||
dst[len] = c;
|
||||
if (!c)
|
||||
break;
|
||||
len++;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
char *strncat(char *dst, const char *src, size_t size)
|
||||
{
|
||||
char *orig = dst;
|
||||
|
||||
while (*dst)
|
||||
dst++;
|
||||
|
||||
while (size && (*dst = *src)) {
|
||||
src++;
|
||||
dst++;
|
||||
size--;
|
||||
}
|
||||
|
||||
*dst = 0;
|
||||
return orig;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
int strncmp(const char *a, const char *b, size_t size)
|
||||
{
|
||||
unsigned int c;
|
||||
int diff = 0;
|
||||
|
||||
while (size-- &&
|
||||
!(diff = (unsigned char)*a++ - (c = (unsigned char)*b++)) && c)
|
||||
;
|
||||
|
||||
return diff;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
char *strncpy(char *dst, const char *src, size_t size)
|
||||
{
|
||||
size_t len;
|
||||
|
||||
for (len = 0; len < size; len++)
|
||||
if ((dst[len] = *src))
|
||||
src++;
|
||||
return dst;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
char *strrchr(const char *s, int c)
|
||||
{
|
||||
const char *ret = NULL;
|
||||
|
||||
while (*s) {
|
||||
if (*s == (char)c)
|
||||
ret = s;
|
||||
s++;
|
||||
}
|
||||
return (char *)ret;
|
||||
}
|
||||
|
||||
/* make sure to include all global symbols */
|
||||
#include "nolibc.h"
|
||||
|
||||
#endif /* _NOLIBC_STRING_H */
|
1432
nolibc/sys.h
Normal file
1432
nolibc/sys.h
Normal file
File diff suppressed because it is too large
Load diff
31
nolibc/time.h
Normal file
31
nolibc/time.h
Normal file
|
@ -0,0 +1,31 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
|
||||
/*
|
||||
* time function definitions for NOLIBC
|
||||
* Copyright (C) 2017-2022 Willy Tarreau <w@1wt.eu>
|
||||
*/
|
||||
|
||||
#ifndef _NOLIBC_TIME_H
|
||||
#define _NOLIBC_TIME_H
|
||||
|
||||
#include "std.h"
|
||||
#include "arch.h"
|
||||
#include "types.h"
|
||||
#include "sys.h"
|
||||
|
||||
static __attribute__((unused))
|
||||
time_t time(time_t *tptr)
|
||||
{
|
||||
struct timeval tv;
|
||||
|
||||
/* note, cannot fail here */
|
||||
sys_gettimeofday(&tv, NULL);
|
||||
|
||||
if (tptr)
|
||||
*tptr = tv.tv_sec;
|
||||
return tv.tv_sec;
|
||||
}
|
||||
|
||||
/* make sure to include all global symbols */
|
||||
#include "nolibc.h"
|
||||
|
||||
#endif /* _NOLIBC_TIME_H */
|
225
nolibc/types.h
Normal file
225
nolibc/types.h
Normal file
|
@ -0,0 +1,225 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
|
||||
/*
|
||||
* Special types used by various syscalls for NOLIBC
|
||||
* Copyright (C) 2017-2021 Willy Tarreau <w@1wt.eu>
|
||||
*/
|
||||
|
||||
#ifndef _NOLIBC_TYPES_H
|
||||
#define _NOLIBC_TYPES_H
|
||||
|
||||
#include "std.h"
|
||||
#include <linux/time.h>
|
||||
#include <linux/stat.h>
|
||||
|
||||
|
||||
/* Only the generic macros and types may be defined here. The arch-specific
|
||||
* ones such as the O_RDONLY and related macros used by fcntl() and open(), or
|
||||
* the layout of sys_stat_struct must not be defined here.
|
||||
*/
|
||||
|
||||
/* stat flags (WARNING, octal here). We need to check for an existing
|
||||
* definition because linux/stat.h may omit to define those if it finds
|
||||
* that any glibc header was already included.
|
||||
*/
|
||||
#if !defined(S_IFMT)
|
||||
#define S_IFDIR 0040000
|
||||
#define S_IFCHR 0020000
|
||||
#define S_IFBLK 0060000
|
||||
#define S_IFREG 0100000
|
||||
#define S_IFIFO 0010000
|
||||
#define S_IFLNK 0120000
|
||||
#define S_IFSOCK 0140000
|
||||
#define S_IFMT 0170000
|
||||
|
||||
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
|
||||
#define S_ISCHR(mode) (((mode) & S_IFMT) == S_IFCHR)
|
||||
#define S_ISBLK(mode) (((mode) & S_IFMT) == S_IFBLK)
|
||||
#define S_ISREG(mode) (((mode) & S_IFMT) == S_IFREG)
|
||||
#define S_ISFIFO(mode) (((mode) & S_IFMT) == S_IFIFO)
|
||||
#define S_ISLNK(mode) (((mode) & S_IFMT) == S_IFLNK)
|
||||
#define S_ISSOCK(mode) (((mode) & S_IFMT) == S_IFSOCK)
|
||||
|
||||
#define S_IRWXU 00700
|
||||
#define S_IRUSR 00400
|
||||
#define S_IWUSR 00200
|
||||
#define S_IXUSR 00100
|
||||
|
||||
#define S_IRWXG 00070
|
||||
#define S_IRGRP 00040
|
||||
#define S_IWGRP 00020
|
||||
#define S_IXGRP 00010
|
||||
|
||||
#define S_IRWXO 00007
|
||||
#define S_IROTH 00004
|
||||
#define S_IWOTH 00002
|
||||
#define S_IXOTH 00001
|
||||
#endif
|
||||
|
||||
/* dirent types */
|
||||
#define DT_UNKNOWN 0x0
|
||||
#define DT_FIFO 0x1
|
||||
#define DT_CHR 0x2
|
||||
#define DT_DIR 0x4
|
||||
#define DT_BLK 0x6
|
||||
#define DT_REG 0x8
|
||||
#define DT_LNK 0xa
|
||||
#define DT_SOCK 0xc
|
||||
|
||||
/* commonly an fd_set represents 256 FDs */
|
||||
#ifndef FD_SETSIZE
|
||||
#define FD_SETSIZE 256
|
||||
#endif
|
||||
|
||||
/* PATH_MAX and MAXPATHLEN are often used and found with plenty of different
|
||||
* values.
|
||||
*/
|
||||
#ifndef PATH_MAX
|
||||
#define PATH_MAX 4096
|
||||
#endif
|
||||
|
||||
#ifndef MAXPATHLEN
|
||||
#define MAXPATHLEN (PATH_MAX)
|
||||
#endif
|
||||
|
||||
/* whence values for lseek() */
|
||||
#define SEEK_SET 0
|
||||
#define SEEK_CUR 1
|
||||
#define SEEK_END 2
|
||||
|
||||
/* Macros used on waitpid()'s return status */
|
||||
#define WEXITSTATUS(status) (((status) & 0xff00) >> 8)
|
||||
#define WIFEXITED(status) (((status) & 0x7f) == 0)
|
||||
#define WTERMSIG(status) ((status) & 0x7f)
|
||||
#define WIFSIGNALED(status) ((status) - 1 < 0xff)
|
||||
|
||||
/* waitpid() flags */
|
||||
#define WNOHANG 1
|
||||
|
||||
/* standard exit() codes */
|
||||
#define EXIT_SUCCESS 0
|
||||
#define EXIT_FAILURE 1
|
||||
|
||||
#define FD_SETIDXMASK (8 * sizeof(unsigned long))
|
||||
#define FD_SETBITMASK (8 * sizeof(unsigned long)-1)
|
||||
|
||||
/* for select() */
|
||||
typedef struct {
|
||||
unsigned long fds[(FD_SETSIZE + FD_SETBITMASK) / FD_SETIDXMASK];
|
||||
} fd_set;
|
||||
|
||||
#define FD_CLR(fd, set) do { \
|
||||
fd_set *__set = (set); \
|
||||
int __fd = (fd); \
|
||||
if (__fd >= 0) \
|
||||
__set->fds[__fd / FD_SETIDXMASK] &= \
|
||||
~(1U << (__fd & FX_SETBITMASK)); \
|
||||
} while (0)
|
||||
|
||||
#define FD_SET(fd, set) do { \
|
||||
fd_set *__set = (set); \
|
||||
int __fd = (fd); \
|
||||
if (__fd >= 0) \
|
||||
__set->fds[__fd / FD_SETIDXMASK] |= \
|
||||
1 << (__fd & FD_SETBITMASK); \
|
||||
} while (0)
|
||||
|
||||
#define FD_ISSET(fd, set) ({ \
|
||||
fd_set *__set = (set); \
|
||||
int __fd = (fd); \
|
||||
int __r = 0; \
|
||||
if (__fd >= 0) \
|
||||
__r = !!(__set->fds[__fd / FD_SETIDXMASK] & \
|
||||
1U << (__fd & FD_SET_BITMASK)); \
|
||||
__r; \
|
||||
})
|
||||
|
||||
#define FD_ZERO(set) do { \
|
||||
fd_set *__set = (set); \
|
||||
int __idx; \
|
||||
int __size = (FD_SETSIZE+FD_SETBITMASK) / FD_SETIDXMASK;\
|
||||
for (__idx = 0; __idx < __size; __idx++) \
|
||||
__set->fds[__idx] = 0; \
|
||||
} while (0)
|
||||
|
||||
/* for poll() */
|
||||
#define POLLIN 0x0001
|
||||
#define POLLPRI 0x0002
|
||||
#define POLLOUT 0x0004
|
||||
#define POLLERR 0x0008
|
||||
#define POLLHUP 0x0010
|
||||
#define POLLNVAL 0x0020
|
||||
|
||||
struct pollfd {
|
||||
int fd;
|
||||
short int events;
|
||||
short int revents;
|
||||
};
|
||||
|
||||
/* for getdents64() */
|
||||
struct linux_dirent64 {
|
||||
uint64_t d_ino;
|
||||
int64_t d_off;
|
||||
unsigned short d_reclen;
|
||||
unsigned char d_type;
|
||||
char d_name[];
|
||||
};
|
||||
|
||||
/* needed by wait4() */
|
||||
struct rusage {
|
||||
struct timeval ru_utime;
|
||||
struct timeval ru_stime;
|
||||
long ru_maxrss;
|
||||
long ru_ixrss;
|
||||
long ru_idrss;
|
||||
long ru_isrss;
|
||||
long ru_minflt;
|
||||
long ru_majflt;
|
||||
long ru_nswap;
|
||||
long ru_inblock;
|
||||
long ru_oublock;
|
||||
long ru_msgsnd;
|
||||
long ru_msgrcv;
|
||||
long ru_nsignals;
|
||||
long ru_nvcsw;
|
||||
long ru_nivcsw;
|
||||
};
|
||||
|
||||
/* The format of the struct as returned by the libc to the application, which
|
||||
* significantly differs from the format returned by the stat() syscall flavours.
|
||||
*/
|
||||
struct stat {
|
||||
dev_t st_dev; /* ID of device containing file */
|
||||
ino_t st_ino; /* inode number */
|
||||
mode_t st_mode; /* protection */
|
||||
nlink_t st_nlink; /* number of hard links */
|
||||
uid_t st_uid; /* user ID of owner */
|
||||
gid_t st_gid; /* group ID of owner */
|
||||
dev_t st_rdev; /* device ID (if special file) */
|
||||
off_t st_size; /* total size, in bytes */
|
||||
blksize_t st_blksize; /* blocksize for file system I/O */
|
||||
blkcnt_t st_blocks; /* number of 512B blocks allocated */
|
||||
union { time_t st_atime; struct timespec st_atim; }; /* time of last access */
|
||||
union { time_t st_mtime; struct timespec st_mtim; }; /* time of last modification */
|
||||
union { time_t st_ctime; struct timespec st_ctim; }; /* time of last status change */
|
||||
};
|
||||
|
||||
/* WARNING, it only deals with the 4096 first majors and 256 first minors */
|
||||
#define makedev(major, minor) ((dev_t)((((major) & 0xfff) << 8) | ((minor) & 0xff)))
|
||||
#define major(dev) ((unsigned int)(((dev) >> 8) & 0xfff))
|
||||
#define minor(dev) ((unsigned int)(((dev) & 0xff))
|
||||
|
||||
#ifndef offsetof
|
||||
#define offsetof(TYPE, FIELD) ((size_t) &((TYPE *)0)->FIELD)
|
||||
#endif
|
||||
|
||||
#ifndef container_of
|
||||
#define container_of(PTR, TYPE, FIELD) ({ \
|
||||
__typeof__(((TYPE *)0)->FIELD) *__FIELD_PTR = (PTR); \
|
||||
(TYPE *)((char *) __FIELD_PTR - offsetof(TYPE, FIELD)); \
|
||||
})
|
||||
#endif
|
||||
|
||||
/* make sure to include all global symbols */
|
||||
#include "nolibc.h"
|
||||
|
||||
#endif /* _NOLIBC_TYPES_H */
|
77
nolibc/unistd.h
Normal file
77
nolibc/unistd.h
Normal file
|
@ -0,0 +1,77 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
|
||||
/*
|
||||
* unistd function definitions for NOLIBC
|
||||
* Copyright (C) 2017-2022 Willy Tarreau <w@1wt.eu>
|
||||
*/
|
||||
|
||||
#ifndef _NOLIBC_UNISTD_H
|
||||
#define _NOLIBC_UNISTD_H
|
||||
|
||||
#include "std.h"
|
||||
#include "arch.h"
|
||||
#include "types.h"
|
||||
#include "sys.h"
|
||||
|
||||
|
||||
#define STDIN_FILENO 0
|
||||
#define STDOUT_FILENO 1
|
||||
#define STDERR_FILENO 2
|
||||
|
||||
|
||||
static __attribute__((unused))
|
||||
int msleep(unsigned int msecs)
|
||||
{
|
||||
struct timeval my_timeval = { msecs / 1000, (msecs % 1000) * 1000 };
|
||||
|
||||
if (sys_select(0, 0, 0, 0, &my_timeval) < 0)
|
||||
return (my_timeval.tv_sec * 1000) +
|
||||
(my_timeval.tv_usec / 1000) +
|
||||
!!(my_timeval.tv_usec % 1000);
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
unsigned int sleep(unsigned int seconds)
|
||||
{
|
||||
struct timeval my_timeval = { seconds, 0 };
|
||||
|
||||
if (sys_select(0, 0, 0, 0, &my_timeval) < 0)
|
||||
return my_timeval.tv_sec + !!my_timeval.tv_usec;
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
int usleep(unsigned int usecs)
|
||||
{
|
||||
struct timeval my_timeval = { usecs / 1000000, usecs % 1000000 };
|
||||
|
||||
return sys_select(0, 0, 0, 0, &my_timeval);
|
||||
}
|
||||
|
||||
static __attribute__((unused))
|
||||
int tcsetpgrp(int fd, pid_t pid)
|
||||
{
|
||||
return ioctl(fd, TIOCSPGRP, &pid);
|
||||
}
|
||||
|
||||
#define _syscall(N, ...) \
|
||||
({ \
|
||||
long _ret = my_syscall##N(__VA_ARGS__); \
|
||||
if (_ret < 0) { \
|
||||
SET_ERRNO(-_ret); \
|
||||
_ret = -1; \
|
||||
} \
|
||||
_ret; \
|
||||
})
|
||||
|
||||
#define _syscall_narg(...) __syscall_narg(__VA_ARGS__, 6, 5, 4, 3, 2, 1, 0)
|
||||
#define __syscall_narg(_0, _1, _2, _3, _4, _5, _6, N, ...) N
|
||||
#define _syscall_n(N, ...) _syscall(N, __VA_ARGS__)
|
||||
#define syscall(...) _syscall_n(_syscall_narg(__VA_ARGS__), ##__VA_ARGS__)
|
||||
|
||||
/* make sure to include all global symbols */
|
||||
#include "nolibc.h"
|
||||
|
||||
#endif /* _NOLIBC_UNISTD_H */
|
93
prep.sh
Executable file
93
prep.sh
Executable file
|
@ -0,0 +1,93 @@
|
|||
#! /bin/bash
|
||||
|
||||
# Paths
|
||||
SOURCE_PATHS_C="src/*.c"
|
||||
SOURCE_PATHS_CXX="src/*.cpp"
|
||||
SOURCE_PATHS_ASM="src/*.s"
|
||||
INCLUDE_PATHS="include include/compat nolibc /usr/include/efi /usr/include/efi/x86_64 /usr/include/efi/protocol"
|
||||
|
||||
# Config
|
||||
ARCH="x86_64" # Warning: Also specified in Makefile_cst
|
||||
COMPILER="gcc"
|
||||
COMPILER_FLAGS="-DEFI_FUNCTION_WRAPPER -DLINUX_UEFI_USE_INTERNAL_INTS -mno-red-zone -fno-stack-protector -fpic -fshort-wchar -Wno-builtin-declaration-mismatch"
|
||||
COMPILER_FLAGS_CXX="-fno-rtti -nostdinc++"
|
||||
ASSEMBLER="nasm"
|
||||
ASSEMBLER_FLAGS=""
|
||||
ASSEMBLER_TARGET="elf64"
|
||||
LINKER="ld"
|
||||
LINKER_FLAGS="-znocombreloc -T /usr/lib/elf_${ARCH}_efi.lds -shared -Bsymbolic -L /usr/lib /usr/lib/crt0-efi-${ARCH}.o -lefi -lgnuefi -nostdlib"
|
||||
LINKER_TARGET="elf_x86_64"
|
||||
|
||||
# Library functions
|
||||
function file_continue {
|
||||
test -f "$1" || echo continue
|
||||
}
|
||||
function getoutfile {
|
||||
echo obj/"$(basename "${1%.*}")-"${1##*.}"".o
|
||||
}
|
||||
function makeentry_init {
|
||||
srcfile="$1"
|
||||
outfile="$(getoutfile $srcfile)"
|
||||
outfiles+=("$outfile")
|
||||
echo " - ${srcfile} produces ${outfile}..." > /dev/stderr
|
||||
}
|
||||
|
||||
# Templates
|
||||
function makeentry_cfile {
|
||||
makeentry_init "$1"
|
||||
echo "
|
||||
${outfile}: ${srcfile}
|
||||
${COMPILER} ${COMPILER_FLAGS} ${COMPILER_INCLUEDIRS} -c ${srcfile} -o ${outfile}"
|
||||
}
|
||||
function makeentry_cppfile {
|
||||
makeentry_init "$1"
|
||||
echo "
|
||||
${outfile}: ${srcfile}
|
||||
${COMPILER} -std=c++17 ${COMPILER_FLAGS} ${COMPILER_FLAGS_CXX} ${COMPILER_INCLUEDIRS} -c ${srcfile} -o ${outfile}"
|
||||
}
|
||||
function makeentry_sfile {
|
||||
makeentry_init "$1"
|
||||
echo "
|
||||
${outfile}: ${srcfile}
|
||||
${ASSEMBLER} ${ASSEMBLER_FLAGS} -f ${ASSEMBLER_TARGET} ${srcfile} -o ${outfile}"
|
||||
}
|
||||
function makeentry_misc {
|
||||
echo "all: link"
|
||||
echo "compile: ${outfiles[@]}"
|
||||
echo "link: compile
|
||||
${LINKER} -o bin/$proj_name -m ${LINKER_TARGET} ${outfiles[@]} ${LINKER_FLAGS}"
|
||||
echo "clean:
|
||||
rm -f ${outfiles[@]} bin/$proj_name"
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Create variables
|
||||
declare -a outfiles
|
||||
proj_name="efitest"
|
||||
|
||||
# Prepare structure
|
||||
mkdir -p obj bin
|
||||
|
||||
# Generate Makefile
|
||||
for path in $INCLUDE_PATHS; do
|
||||
COMPILER_INCLUEDIRS="$COMPILER_INCLUEDIRS -I${path} "
|
||||
done
|
||||
for filename in $SOURCE_PATHS_C; do
|
||||
$(file_continue "$filename")
|
||||
makeentry_cfile "$filename" >> Makefile_body
|
||||
done
|
||||
for filename in $SOURCE_PATHS_CXX; do
|
||||
$(file_continue "$filename")
|
||||
makeentry_cppfile "$filename" >> Makefile_body
|
||||
done
|
||||
for filename in $SOURCE_PATHS_ASM; do
|
||||
$(file_continue "$filename")
|
||||
makeentry_sfile "$filename" >> Makefile_body
|
||||
done
|
||||
makeentry_misc > Makefile
|
||||
cat Makefile_body >> Makefile
|
||||
if [ -f "Makefile_cst" ] ; then
|
||||
echo >> Makefile
|
||||
cat Makefile_cst >> Makefile
|
||||
fi
|
||||
rm -f Makefile_body
|
762
src/linux-uefi.c
Normal file
762
src/linux-uefi.c
Normal file
|
@ -0,0 +1,762 @@
|
|||
/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
|
||||
/*
|
||||
* Linux-compatible wrapper around UEFI
|
||||
* Copyright (C) 2023 Nils Sauer <tuxifan@posteo.de>
|
||||
*/
|
||||
|
||||
#ifndef LINUX_UEFI_USE_INTERNAL_INTS
|
||||
# define LINUX_UEFI_USE_INTERNAL_INTS
|
||||
#endif /* LINUX_UEFI_USE_INTERNAL_INTS */
|
||||
#include "linux-uefi.h"
|
||||
#include "linux-uefi-syscalls.h"
|
||||
|
||||
#include <efi.h>
|
||||
#include <efilib.h>
|
||||
|
||||
|
||||
/* Macros */
|
||||
#define MAX_FDS 16
|
||||
#define VIRTUAL_FD_BASE 4
|
||||
#define TO_REAL_FD(fd) (fd-VIRTUAL_FD_BASE)
|
||||
#define TO_VIRTUAL_FD(fd) (fd+VIRTUAL_FD_BASE)
|
||||
|
||||
#define EMU_PID 2
|
||||
#define EMU_UID 1000
|
||||
#define EMU_GID 1000
|
||||
|
||||
|
||||
/* Global variables */
|
||||
char *environ[] = {NULL};
|
||||
const unsigned long _auxv[] = {0};
|
||||
|
||||
/* startup code and handles */
|
||||
static EFI_HANDLE Image;
|
||||
static EFI_FILE_HANDLE Volume;
|
||||
static EFI_FILE_HANDLE fds[MAX_FDS];
|
||||
|
||||
extern
|
||||
int main(int argc, char **argv);
|
||||
|
||||
static
|
||||
EFI_FILE_HANDLE GetVolume(EFI_HANDLE image);
|
||||
|
||||
EFI_STATUS EFIAPI efi_main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) {
|
||||
InitializeLib(ImageHandle, SystemTable);
|
||||
|
||||
Image = ImageHandle;
|
||||
Volume = GetVolume(ImageHandle);
|
||||
|
||||
for (unsigned it = 0; it != MAX_FDS; it++) {
|
||||
fds[it] = (EFI_FILE_HANDLE)0;
|
||||
}
|
||||
|
||||
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"Initialized! Calling main()...\r\n");
|
||||
|
||||
/* Stub argv, argc for now */
|
||||
char *argv[1] = {"main.efi"};
|
||||
|
||||
return main(1, argv)?EFI_END_OF_FILE/*?*/:EFI_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
/* Tons of tiny little helper functions */
|
||||
static
|
||||
EFI_FILE_HANDLE GetVolume(EFI_HANDLE image) {
|
||||
EFI_LOADED_IMAGE *loaded_image = NULL; /* image interface */
|
||||
EFI_GUID lipGuid = EFI_LOADED_IMAGE_PROTOCOL_GUID; /* image interface GUID */
|
||||
EFI_FILE_IO_INTERFACE *IOVolume; /* file system interface */
|
||||
EFI_GUID fsGuid = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID; /* file system interface GUID */
|
||||
EFI_FILE_HANDLE Volume; /* the volume's interface */
|
||||
|
||||
/* get the loaded image protocol interface for our "image" */
|
||||
uefi_call_wrapper(BS->HandleProtocol, 3, image, &lipGuid, (void **) &loaded_image);
|
||||
/* get the volume handle */
|
||||
uefi_call_wrapper(BS->HandleProtocol, 3, loaded_image->DeviceHandle, &fsGuid, (VOID*)&IOVolume);
|
||||
uefi_call_wrapper(IOVolume->OpenVolume, 2, IOVolume, &Volume);
|
||||
return Volume;
|
||||
}
|
||||
|
||||
static
|
||||
CHAR16 *bstr_to_wstr(const char *input) {
|
||||
static char fres[1024];
|
||||
unsigned it;
|
||||
for (it = 0; input[it] && it != sizeof(fres)/2 - 2; it++) {
|
||||
fres[it*2] = input[it];
|
||||
fres[it*2+1] = '\0';
|
||||
}
|
||||
fres[it*2] = '\0';
|
||||
fres[it*2+1] = '\0';
|
||||
|
||||
return (CHAR16 *)fres;
|
||||
}
|
||||
|
||||
static
|
||||
int efi_status_to_errno(EFI_STATUS status) {
|
||||
switch((int)(status & 0xffff)) {
|
||||
case EFI_WRITE_PROTECTED & 0xffff: return EROFS;
|
||||
case EFI_ACCESS_DENIED & 0xffff: return EACCES;
|
||||
case EFI_VOLUME_FULL & 0xffff: return ENOSPC;
|
||||
case EFI_NOT_FOUND & 0xffff: return ENOENT;
|
||||
case EFI_INVALID_PARAMETER & 0xffff: return EINVAL;
|
||||
default: return EIO;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* "Process" handling */
|
||||
void proc_exit(int status) {
|
||||
uefi_call_wrapper(BS->Exit, 4, Image, status?EFI_END_OF_FILE/*?*/:EFI_SUCCESS, 0, NULL);
|
||||
}
|
||||
|
||||
void proc_abort() {
|
||||
uefi_call_wrapper(BS->Exit, 4, Image, EFI_ABORTED, 0, NULL);
|
||||
}
|
||||
|
||||
int proc_kill(pid_t pid, int sig) {
|
||||
if (pid == EMU_PID - 1)
|
||||
return -EPERM;
|
||||
|
||||
if (pid != EMU_PID)
|
||||
return -ESRCH;
|
||||
|
||||
if (sig >= 30)
|
||||
return -EINVAL;
|
||||
|
||||
/* TODO: Signal handling! */
|
||||
|
||||
proc_abort();
|
||||
|
||||
while (1); /* Don't want the compiler to complain */
|
||||
}
|
||||
|
||||
|
||||
/* "Power" handling */
|
||||
int power_reboot(int magic1, int magic2, int cmd, void *arg) {
|
||||
if (magic1 != LINUX_REBOOT_MAGIC1)
|
||||
return -EINVAL;
|
||||
if (magic2 != LINUX_REBOOT_MAGIC2 &&
|
||||
magic2 != LINUX_REBOOT_MAGIC2A &&
|
||||
magic2 != LINUX_REBOOT_MAGIC2B &&
|
||||
magic2 != LINUX_REBOOT_MAGIC2C)
|
||||
return -EINVAL;
|
||||
|
||||
if (cmd == LINUX_REBOOT_CMD_CAD_OFF ||
|
||||
cmd == LINUX_REBOOT_CMD_CAD_ON)
|
||||
return -EINVAL; /* TODO: Implement this! */
|
||||
|
||||
if (cmd == LINUX_REBOOT_CMD_HALT) {
|
||||
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"\r\nSystem halted.");
|
||||
while (1) asm("cli; hlt"); /* TODO: Make this more portable */
|
||||
}
|
||||
|
||||
if (cmd == LINUX_REBOOT_CMD_KEXEC)
|
||||
return -EINVAL; /* TODO: Implement this! */
|
||||
|
||||
if (cmd == LINUX_REBOOT_CMD_SW_SUSPEND)
|
||||
return -EINVAL; /* Won't ever be implemented */
|
||||
|
||||
if (cmd == LINUX_REBOOT_CMD_POWER_OFF) {
|
||||
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"\r\nPower down.");
|
||||
uefi_call_wrapper(RT->ResetSystem, 4, EfiResetShutdown, EFI_SUCCESS, 34*2, L"LINUX_REBOOT_CMD_POWER_OFF called");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (cmd == LINUX_REBOOT_CMD_RESTART) {
|
||||
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"\r\nRestarting system.");
|
||||
uefi_call_wrapper(RT->ResetSystem, 4, EfiResetWarm, EFI_SUCCESS, 32, L"LINUX_REBOOT_CMD_RESTART called");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (cmd == LINUX_REBOOT_CMD_RESTART2) {
|
||||
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"\r\nRestarting system with command '");
|
||||
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, bstr_to_wstr((const char*)arg));
|
||||
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, L"'");
|
||||
uefi_call_wrapper(RT->ResetSystem, 4, EfiResetWarm, EFI_SUCCESS, 33, L"LINUX_REBOOT_CMD_RESTART2 called");
|
||||
return 0;
|
||||
}
|
||||
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
|
||||
/* Timekeeping functions */
|
||||
static
|
||||
/* from musl */
|
||||
uint64_t time_year_to_secs(uint64_t year, int *is_leap) {
|
||||
int y, cycles, centuries, leaps, rem;
|
||||
|
||||
if (year-2ULL <= 136) {
|
||||
y = (int)year;
|
||||
leaps = (y-68)>>2;
|
||||
if (!((y-68)&3)) {
|
||||
leaps--;
|
||||
if (is_leap) *is_leap = 1;
|
||||
} else if (is_leap) *is_leap = 0;
|
||||
return 31536000ULL*(uint64_t)(y-70) + 86400ULL*(uint64_t)leaps;
|
||||
}
|
||||
|
||||
static int static_is_leap = 0;
|
||||
if (!is_leap) is_leap = &static_is_leap;
|
||||
cycles = (int)((year-100) / 400);
|
||||
rem = (year-100) % 400;
|
||||
if (rem < 0) {
|
||||
cycles--;
|
||||
rem += 400;
|
||||
}
|
||||
if (!rem) {
|
||||
*is_leap = 1;
|
||||
centuries = 0;
|
||||
leaps = 0;
|
||||
} else {
|
||||
if (rem >= 200) {
|
||||
if (rem >= 300) { centuries = 3; rem -= 300; }
|
||||
else { centuries = 2; rem -= 200; }
|
||||
} else {
|
||||
if (rem >= 100) { centuries = 1; rem -= 100; }
|
||||
else centuries = 0;
|
||||
}
|
||||
if (!rem) {
|
||||
*is_leap = 0;
|
||||
leaps = 0;
|
||||
} else {
|
||||
leaps = rem / 4;
|
||||
rem %= 4;
|
||||
*is_leap = !rem;
|
||||
}
|
||||
}
|
||||
|
||||
leaps += 97*cycles + 24*centuries - *is_leap;
|
||||
|
||||
return (uint64_t)(year-100) * 31536000ULL + (uint64_t)leaps * 86400ULL + 946684800ULL + 86400ULL;
|
||||
}
|
||||
|
||||
static
|
||||
/* adpoted from posix-uefi */
|
||||
time_t time_efi_time_to_time_t(EFI_TIME *time) {
|
||||
time->Year += /* workaround some buggy firmware*/ time->Year > 2000 ? -1900 : 98;
|
||||
time->Month -= 1;
|
||||
|
||||
static const uint64_t secs_through_month[] = {
|
||||
0, 31*86400, 59*86400, 90*86400,
|
||||
120*86400, 151*86400, 181*86400, 212*86400,
|
||||
243*86400, 273*86400, 304*86400, 334*86400 };
|
||||
int is_leap;
|
||||
uint64_t year = (uint64_t)time->Year, t, adj;
|
||||
int month = time->Month;
|
||||
if (month >= 12 || month < 0) {
|
||||
adj = (uint64_t)month / 12;
|
||||
month %= 12;
|
||||
if (month < 0) {
|
||||
adj--;
|
||||
month += 12;
|
||||
}
|
||||
year += adj;
|
||||
}
|
||||
t = time_year_to_secs(year, &is_leap);
|
||||
t += secs_through_month[month];
|
||||
if (is_leap && month >= 2) t += 86400;
|
||||
t += 86400ULL * (uint64_t)(time->Day-1);
|
||||
t += 3600ULL * (uint64_t)time->Hour;
|
||||
t += 60ULL * (uint64_t)time->Minute;
|
||||
t += (uint64_t)time->Second;
|
||||
return (time_t)t;
|
||||
}
|
||||
|
||||
static
|
||||
time_t time_cstd_time(time_t *tloc) {
|
||||
EFI_TIME efi_time = {0};
|
||||
uefi_call_wrapper(ST->RuntimeServices->GetTime, 2, &efi_time, NULL);
|
||||
time_t ret = time_efi_time_to_time_t(&efi_time);
|
||||
if (tloc)
|
||||
*tloc = ret;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int time_gettimeofday(struct timeval *tv) {
|
||||
tv->tv_sec = time_cstd_time(NULL);
|
||||
tv->tv_usec = 0;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int time_sleep(struct timeval *duration) {
|
||||
EFI_STATUS status = uefi_call_wrapper(BS->Stall, 1, duration->tv_sec * 1000000UL + duration->tv_usec);
|
||||
if (EFI_ERROR(status))
|
||||
return -efi_status_to_errno(status);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* Basic I/O */
|
||||
static
|
||||
int fd_real_find_free() {
|
||||
for (unsigned it = 0; it != MAX_FDS; it++) {
|
||||
if (fds[it])
|
||||
return it;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
static
|
||||
int fd_real_for_handle(EFI_FILE_HANDLE FileHandle) {
|
||||
for (unsigned it = 0; it != MAX_FDS; it++) {
|
||||
if (fds[it] == FileHandle)
|
||||
return it;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
inline static
|
||||
void fd_real_reset(int fd) {
|
||||
fds[fd] = (EFI_FILE_HANDLE)0;
|
||||
}
|
||||
|
||||
static
|
||||
EFI_STATUS fd_efi_get_file_info(EFI_FILE_HANDLE FileHandle, EFI_FILE_INFO *info) {
|
||||
EFI_GUID infoGuid = EFI_FILE_INFO_ID;
|
||||
UINTN fsiz = sizeof(EFI_FILE_INFO);
|
||||
|
||||
return uefi_call_wrapper(FileHandle->GetInfo, 4, FileHandle, &infoGuid, &fsiz, info);
|
||||
}
|
||||
static
|
||||
EFI_STATUS fd_efi_set_file_info(EFI_FILE_HANDLE FileHandle, const EFI_FILE_INFO *info) {
|
||||
EFI_GUID infoGuid = EFI_FILE_INFO_ID;
|
||||
UINTN fsiz = sizeof(EFI_FILE_INFO);
|
||||
|
||||
return uefi_call_wrapper(FileHandle->SetInfo, 4, FileHandle, &infoGuid, fsiz, &info);
|
||||
}
|
||||
|
||||
int fd_open(const char *pathname, int flags) {
|
||||
int fd = fd_real_find_free();
|
||||
|
||||
if (fd < 0)
|
||||
return -EMFILE;
|
||||
|
||||
EFI_FILE_HANDLE FileHandle;
|
||||
|
||||
/* Always read mode to avoid firmware bugs */
|
||||
UINT64 OpenMode = EFI_FILE_MODE_READ;
|
||||
if (flags & O_APPEND)
|
||||
OpenMode |= EFI_FILE_MODE_WRITE;
|
||||
if (flags & O_CREAT)
|
||||
OpenMode |= EFI_FILE_MODE_CREATE;
|
||||
if (flags & O_WRONLY || flags & O_RDWR)
|
||||
OpenMode |= EFI_FILE_MODE_WRITE;
|
||||
|
||||
EFI_STATUS status = uefi_call_wrapper(Volume->Open, 5, Volume, &FileHandle, bstr_to_wstr(pathname), OpenMode, (flags & O_DIRECTORY)?EFI_FILE_DIRECTORY:0);
|
||||
if (EFI_ERROR(status))
|
||||
return -efi_status_to_errno(status);
|
||||
|
||||
EFI_FILE_INFO info;
|
||||
status = fd_efi_get_file_info(FileHandle, &info);
|
||||
if (EFI_ERROR(status))
|
||||
return -efi_status_to_errno(status);
|
||||
|
||||
if (flags & O_APPEND) {
|
||||
uefi_call_wrapper(FileHandle->SetPosition, 2, FileHandle, 0xFFFFFFFFFFFFFFFF);
|
||||
} else {
|
||||
if (OpenMode & EFI_FILE_MODE_WRITE) {
|
||||
info.FileSize = 0;
|
||||
fd_efi_set_file_info(FileHandle, &info);
|
||||
}
|
||||
}
|
||||
if (flags & O_DIRECTORY) {
|
||||
if (!(info.Attribute & EFI_FILE_DIRECTORY))
|
||||
return -ENOTDIR;
|
||||
} else {
|
||||
if (info.Attribute & EFI_FILE_DIRECTORY)
|
||||
return -EISDIR;
|
||||
}
|
||||
|
||||
return TO_VIRTUAL_FD(fd);
|
||||
}
|
||||
|
||||
int fd_close(int fd) {
|
||||
if (fd > MAX_FDS)
|
||||
return -EBADF;
|
||||
if (fd < VIRTUAL_FD_BASE)
|
||||
return -EACCES;
|
||||
|
||||
EFI_FILE_HANDLE FileHandle = fds[TO_REAL_FD(fd)];
|
||||
if (FileHandle == (EFI_FILE_HANDLE)0)
|
||||
return -EBADF;
|
||||
|
||||
fd_real_reset(TO_REAL_FD(fd));
|
||||
|
||||
if (!fd_real_for_handle(FileHandle)) {
|
||||
EFI_STATUS status = uefi_call_wrapper(FileHandle->Close, 1, FileHandle);
|
||||
if (EFI_ERROR(status))
|
||||
return -efi_status_to_errno(status);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fd_fsync(int fd) {
|
||||
if (fd > MAX_FDS)
|
||||
return -EBADF;
|
||||
if (fd < VIRTUAL_FD_BASE)
|
||||
return -EACCES;
|
||||
|
||||
EFI_FILE_HANDLE FileHandle = fds[TO_REAL_FD(fd)];
|
||||
if (FileHandle == (EFI_FILE_HANDLE)0)
|
||||
return -EBADF;
|
||||
|
||||
EFI_STATUS status = uefi_call_wrapper(FileHandle->Flush, 1, FileHandle);
|
||||
if (EFI_ERROR(status))
|
||||
return -efi_status_to_errno(status);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fd_dup(int old_fd) {
|
||||
if (old_fd > MAX_FDS)
|
||||
return -EBADF;
|
||||
if (old_fd < VIRTUAL_FD_BASE) /* can't dup stdandard I/O! */
|
||||
return -EACCES;
|
||||
|
||||
EFI_FILE_HANDLE FileHandle = fds[TO_REAL_FD(old_fd)];
|
||||
if (FileHandle == (EFI_FILE_HANDLE)0)
|
||||
return -EBADF;
|
||||
|
||||
int new_fd = fd_real_find_free();
|
||||
|
||||
if (new_fd < 0)
|
||||
return -EMFILE;
|
||||
|
||||
fds[new_fd] = FileHandle;
|
||||
|
||||
return TO_VIRTUAL_FD(new_fd);
|
||||
}
|
||||
|
||||
int fd_dup2(int old_fd, int new_fd) {
|
||||
if (old_fd > MAX_FDS)
|
||||
return -EBADF;
|
||||
if (old_fd < VIRTUAL_FD_BASE) /* can't dup stdandard I/O! */
|
||||
return -EACCES;
|
||||
|
||||
if (fds[TO_REAL_FD(new_fd)]) {
|
||||
int res = fd_close(new_fd);
|
||||
if (res < 0)
|
||||
return res;
|
||||
}
|
||||
|
||||
EFI_FILE_HANDLE FileHandle = fds[TO_REAL_FD(old_fd)];
|
||||
if (FileHandle == (EFI_FILE_HANDLE)0)
|
||||
return -EBADF;
|
||||
|
||||
if (new_fd < 0)
|
||||
return -EMFILE;
|
||||
|
||||
fds[new_fd] = FileHandle;
|
||||
|
||||
return TO_VIRTUAL_FD(new_fd);
|
||||
}
|
||||
|
||||
off_t fd_lseek(int fd, off_t offset, int whence) {
|
||||
if (fd > MAX_FDS)
|
||||
return -EBADF;
|
||||
if (fd < VIRTUAL_FD_BASE)
|
||||
return -EACCES;
|
||||
|
||||
EFI_FILE_HANDLE FileHandle = fds[TO_REAL_FD(fd)];
|
||||
if (FileHandle == (EFI_FILE_HANDLE)0)
|
||||
return -EBADF;
|
||||
|
||||
EFI_STATUS status;
|
||||
UINT64 offset64;
|
||||
switch (whence) {
|
||||
case SEEK_SET: {
|
||||
offset64 = offset;
|
||||
status = uefi_call_wrapper(FileHandle->SetPosition, 2, FileHandle, offset64);
|
||||
} break;
|
||||
case SEEK_END: {
|
||||
status = uefi_call_wrapper(FileHandle->SetPosition, 2, FileHandle, 0xFFFFFFFFFFFFFFFF);
|
||||
if (EFI_ERROR(status))
|
||||
break;
|
||||
} /* fallthrough */
|
||||
case SEEK_CUR: {
|
||||
status = uefi_call_wrapper(FileHandle->GetPosition, 2, FileHandle, &offset64);
|
||||
if (EFI_ERROR(status))
|
||||
break;
|
||||
offset64 += offset;
|
||||
status = uefi_call_wrapper(FileHandle->SetPosition, 2, FileHandle, offset64);
|
||||
} break;
|
||||
}
|
||||
|
||||
if (EFI_ERROR(status))
|
||||
return -efi_status_to_errno(status);
|
||||
|
||||
return offset64;
|
||||
}
|
||||
|
||||
ssize_t fd_read(int fd, void *buf, size_t count) {
|
||||
if (fd > MAX_FDS)
|
||||
return -EBADF;
|
||||
|
||||
/* Standard I/O handling */
|
||||
if (fd == 0) {
|
||||
unsigned it;
|
||||
for (it = 0; it != count; it++) {
|
||||
/* delay 1ms */
|
||||
uefi_call_wrapper(BS->Stall, 1, 1000);
|
||||
/* Wait for key */
|
||||
EFI_EVENT Events[1] = {ST->ConIn->WaitForKey};
|
||||
UINTN index;
|
||||
uefi_call_wrapper(BS->WaitForEvent, 3, 1, Events, &index);
|
||||
/* Read key */
|
||||
EFI_INPUT_KEY key;
|
||||
uefi_call_wrapper(ST->ConIn->ReadKeyStroke, 2, ST->ConIn, &key);
|
||||
/* Get key as character */
|
||||
char c = ((char *)&key.UnicodeChar)[0];
|
||||
/* Avoid backspace if no input */
|
||||
if (it == 0 && c == '\b')
|
||||
continue;
|
||||
/* Print key */
|
||||
CHAR16 print_buf[2] = {key.UnicodeChar, L'\0'};
|
||||
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, print_buf);
|
||||
if (c == '\r') {
|
||||
print_buf[0] = '\n';
|
||||
uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, print_buf);
|
||||
}
|
||||
/* Translate line feed to newline */
|
||||
if (c == '\r')
|
||||
c = '\n';
|
||||
/* Add key to buffer */
|
||||
((char *)buf)[it] = c;
|
||||
/* Stop if linebreak */
|
||||
if (c == '\n')
|
||||
break;
|
||||
}
|
||||
|
||||
return it + 1;
|
||||
}
|
||||
|
||||
if (fd < VIRTUAL_FD_BASE)
|
||||
return -EACCES;
|
||||
|
||||
EFI_FILE_HANDLE FileHandle = fds[TO_REAL_FD(fd)];
|
||||
if (FileHandle == (EFI_FILE_HANDLE)0)
|
||||
return -EBADF;
|
||||
|
||||
EFI_STATUS status = uefi_call_wrapper(FileHandle->Read, 3, FileHandle, &count, buf);
|
||||
if (EFI_ERROR(status))
|
||||
return -efi_status_to_errno(status);
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
ssize_t fd_write(int fd, const void *buf, size_t count) {
|
||||
if (fd > MAX_FDS)
|
||||
return -EBADF;
|
||||
|
||||
/* Standard I/O handling */
|
||||
if (fd == 1 || fd == 2) {
|
||||
const char *src_buf = (const char *)buf;
|
||||
CHAR16 str_buf[count*2];
|
||||
unsigned it;
|
||||
unsigned str_it;
|
||||
for (it = 0, str_it = 0; it != count; it++, str_it++) {
|
||||
if (src_buf[it] == '\0') {
|
||||
str_it--;
|
||||
continue;
|
||||
}
|
||||
if (src_buf[it] == '\n') {
|
||||
str_buf[str_it] = L'\r';
|
||||
str_buf[++str_it] = L'\n';
|
||||
continue;
|
||||
}
|
||||
|
||||
((char*)(str_buf + str_it))[0] = (CHAR16)(src_buf[it]);
|
||||
((char*)(str_buf + str_it))[1] = '\0';
|
||||
}
|
||||
str_buf[str_it] = L'\0';
|
||||
|
||||
EFI_STATUS status = uefi_call_wrapper(ST->ConOut->OutputString, 2, ST->ConOut, str_buf);
|
||||
if (EFI_ERROR(status))
|
||||
return -efi_status_to_errno(status);
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
if (fd < VIRTUAL_FD_BASE)
|
||||
return -EACCES;
|
||||
|
||||
EFI_FILE_HANDLE FileHandle = fds[TO_REAL_FD(fd)];
|
||||
if (FileHandle == (EFI_FILE_HANDLE)0)
|
||||
return -EBADF;
|
||||
|
||||
EFI_STATUS status = uefi_call_wrapper(FileHandle->Write, 3, FileHandle, &count, buf);
|
||||
if (EFI_ERROR(status))
|
||||
return -efi_status_to_errno(status);
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
int fd_getdents64(int fd, struct linux_dirent64 *dirp, int count) {
|
||||
if (fd > MAX_FDS)
|
||||
return -EBADF;
|
||||
if (fd < VIRTUAL_FD_BASE)
|
||||
return -EACCES;
|
||||
|
||||
EFI_FILE_HANDLE FileHandle = fds[TO_REAL_FD(fd)];
|
||||
if (FileHandle == (EFI_FILE_HANDLE)0)
|
||||
return -EBADF;
|
||||
|
||||
size_t fres = 0;
|
||||
|
||||
count = count / sizeof(struct linux_dirent64);
|
||||
for (unsigned it = 0; it != count; it++) {
|
||||
EFI_FILE_INFO info[16]; /* Some cheap padding to avoid heap use, x16 should be plently */
|
||||
UINTN infoSize = sizeof(info);
|
||||
EFI_STATUS status = uefi_call_wrapper(FileHandle->Read, 3, FileHandle, &infoSize, info);
|
||||
if (EFI_ERROR(status))
|
||||
return -efi_status_to_errno(status);
|
||||
|
||||
struct linux_dirent64 *dire = dirp + count;
|
||||
|
||||
unsigned it;
|
||||
for (it = 0; info->FileName[it]; it++) {
|
||||
char *c = (char*)&info->FileName[it];
|
||||
dire->d_name[it] = *c;
|
||||
}
|
||||
dire->d_name[it] = '\0';
|
||||
|
||||
dire->d_ino = 0; /* UEFI isn't giving us that information */
|
||||
dire->d_off = dire->d_reclen = sizeof(uint64_t) + sizeof(int64_t) + sizeof(unsigned short) + sizeof(unsigned char) + it + 1;
|
||||
|
||||
fres += dire->d_reclen;
|
||||
}
|
||||
|
||||
return fres;
|
||||
}
|
||||
|
||||
static
|
||||
int fd_efi_stat(EFI_FILE_HANDLE FileHandle, struct stat *buf) {
|
||||
buf->st_dev = (unsigned long)Volume;
|
||||
buf->st_ino = 0; /* UEFI won't tell us */
|
||||
buf->st_nlink = 1;
|
||||
buf->st_mode = 0666;
|
||||
buf->st_uid = EMU_UID;
|
||||
buf->st_gid = EMU_GID;
|
||||
buf->st_blksize = 0;
|
||||
buf->st_atim.tv_nsec = 0;
|
||||
buf->st_mtim.tv_nsec = 0;
|
||||
buf->st_ctim.tv_nsec = 0;
|
||||
|
||||
EFI_FILE_INFO info;
|
||||
EFI_STATUS status = fd_efi_get_file_info(FileHandle, &info);
|
||||
if (EFI_ERROR(status))
|
||||
return -efi_status_to_errno(status);
|
||||
|
||||
buf->st_size = info.FileSize;
|
||||
buf->st_blocks = info.PhysicalSize;
|
||||
buf->st_atime = time_efi_time_to_time_t(&info.LastAccessTime);
|
||||
buf->st_mtime = time_efi_time_to_time_t(&info.ModificationTime);
|
||||
buf->st_ctime = time_efi_time_to_time_t(&info.CreateTime);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int fd_fstat(int fd, struct stat *buf) {
|
||||
if (fd > MAX_FDS)
|
||||
return -EBADF;
|
||||
if (fd < VIRTUAL_FD_BASE)
|
||||
return -EACCES;
|
||||
|
||||
EFI_FILE_HANDLE FileHandle = fds[TO_REAL_FD(fd)];
|
||||
if (FileHandle == (EFI_FILE_HANDLE)0)
|
||||
return -EBADF;
|
||||
|
||||
return fd_efi_stat(FileHandle, buf);
|
||||
}
|
||||
|
||||
int fd_stat(const char *path, struct stat *buf) {
|
||||
EFI_FILE_HANDLE FileHandle;
|
||||
EFI_STATUS status = uefi_call_wrapper(Volume->Open, 5, Volume, &FileHandle, bstr_to_wstr(path), EFI_FILE_MODE_READ, 0);
|
||||
if (EFI_ERROR(status))
|
||||
return -efi_status_to_errno(status);
|
||||
|
||||
return fd_efi_stat(FileHandle, buf);
|
||||
}
|
||||
|
||||
int fd_select(int nfds, uint64_t *readfds, uint64_t *writefds, uint64_t *exceptfds, struct timeval *timeout) {
|
||||
if (nfds || readfds || writefds || exceptfds)
|
||||
return ENOTSUP;
|
||||
|
||||
return time_sleep(timeout);
|
||||
}
|
||||
|
||||
|
||||
/* Memory management functions */
|
||||
void *mem_mmap(void *addr, size_t length, int prot, int flags, int fd, off_t offset) {
|
||||
(void)addr;
|
||||
(void)prot;
|
||||
(void)flags;
|
||||
(void)offset;
|
||||
|
||||
if (fd > 0)
|
||||
return (void *)(long)-ENOTSUP;
|
||||
|
||||
void *fres;
|
||||
|
||||
EFI_STATUS status = uefi_call_wrapper(BS->AllocatePool, 3, EfiLoaderData, (UINTN)length, &fres);
|
||||
if (EFI_ERROR(status))
|
||||
return (void *)(long)-efi_status_to_errno(status);
|
||||
|
||||
return fres;
|
||||
}
|
||||
|
||||
int mem_munmap(void *addr, size_t length) {
|
||||
(void)length;
|
||||
|
||||
EFI_STATUS status = uefi_call_wrapper(BS->FreePool, 1, addr);
|
||||
if (EFI_ERROR(status))
|
||||
return -efi_status_to_errno(status);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
/* System call emulation */
|
||||
#pragma GCC diagnostic ignored "-Wint-conversion"
|
||||
uint64_t syscall_emu_x86_64(uint64_t no, uint64_t *args) {
|
||||
switch (no) {
|
||||
case 0: return fd_read(args[0], args[1], args[2]);
|
||||
case 1: return fd_write(args[0], args[1], args[2]);
|
||||
case 2: return fd_open(args[0], args[1]);
|
||||
case 3: return fd_close(args[0]);
|
||||
case 4: return fd_stat(args[0], args[1]);
|
||||
case 5: return fd_fstat(args[0], args[1]);
|
||||
case 6: return fd_stat(args[0], args[1]);
|
||||
case 8: return fd_lseek(args[0], args[1], args[2]);
|
||||
case 9: return mem_mmap(args[0], args[1], args[2], args[3], args[4], args[5]);
|
||||
case 11: return mem_munmap(args[0], args[1]);
|
||||
case 23: return fd_select(args[0], args[1], args[2], args[3], args[4]);
|
||||
case 32: return fd_dup(args[0]);
|
||||
case 33: return fd_dup2(args[0], args[1]);
|
||||
case 60: proc_exit(args[0]); return 0;
|
||||
case 74: return fd_fsync(args[0]);
|
||||
case 75: return fd_fsync(args[0]);
|
||||
case 62: return proc_kill(args[0], args[1]);
|
||||
case 96: return time_gettimeofday(args[0]);
|
||||
case 169: return power_reboot(args[0], args[1], args[2], args[3]);
|
||||
case 217: return fd_getdents64(args[0], args[1], args[2]);
|
||||
|
||||
case 10: return 0; /* At least pretend we care */
|
||||
case 24: return 0; /* There is no multithreading, so nothing to do */
|
||||
case 39: return EMU_PID;
|
||||
case 102: return EMU_UID;
|
||||
case 104: return EMU_GID;
|
||||
case 105: return -EPERM;
|
||||
case 106: return -EPERM;
|
||||
case 107: return EMU_UID;
|
||||
case 108: return EMU_GID;
|
||||
case 110: return EMU_PID-1;
|
||||
case 186: return EMU_PID;
|
||||
|
||||
default: return -ENOSYS;
|
||||
}
|
||||
}
|
42
src/main.c
Normal file
42
src/main.c
Normal file
|
@ -0,0 +1,42 @@
|
|||
#include "getdelim.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
puts("Testing write() to stdout...");
|
||||
for (unsigned it = 0; it != 12; it++) {
|
||||
write(1, "Hello world!\n"+it, 13-it);
|
||||
}
|
||||
|
||||
puts("Testing bad write()...");
|
||||
write(123456, "Hi!", 3);
|
||||
perror("Error");
|
||||
|
||||
puts("Testing printf()...");
|
||||
for (unsigned it = 0; it != 16; it++) {
|
||||
printf("it = %d! ", it);
|
||||
}
|
||||
printf("\n");
|
||||
|
||||
puts("Testing getline() from stdin...");
|
||||
char *input_buf = NULL;
|
||||
size_t input_len;
|
||||
printf("Type something in: ");
|
||||
my_getline(&input_buf, &input_len, 0);
|
||||
input_buf[input_len-1] = '\0';
|
||||
printf("You typed in '%s'\n", input_buf);
|
||||
|
||||
puts("Testing sleep() for 5 seconds...");
|
||||
sleep(5);
|
||||
|
||||
puts("Testing floating point maths...");
|
||||
{int k = 1; float i,j,r,x,y=-16;while(puts(""),y++<15)for(x=0;x++<84;putchar(" .:-;!/>)|&IH%*#"[k&15]))for(i=k=r=0;j=r*r-i*i-2+x/25,i=2*r*i+y/10,j*j+i*i<11&&k++<111;r=j);}
|
||||
sleep(1);
|
||||
|
||||
puts("Press return to exit.");
|
||||
while (getchar() != '\n');
|
||||
}
|
Loading…
Add table
Reference in a new issue