Created
March 15, 2018 21:58
-
-
Save shlevy/36092296b1f8433faff522a39032919b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let qemuBinfmtWrapperSource = pkgs.writeTextDir "qemu-binfmt-wrapper.c" | |
'' | |
#include <unistd.h> | |
#include <string.h> | |
#include <stdio.h> | |
#include <sys/auxv.h> | |
int main(int argc,char ** argv) { | |
int fd = (int) getauxval(AT_EXECFD); | |
size_t sz = snprintf(NULL, 0, "/proc/self/fd/%d", fd) + 1; | |
char fn[sz]; | |
snprintf(fn, sz, "/proc/self/fd/%d", fd); | |
char * realArgv[argc + 2]; | |
realArgv[0] = "qemu-riscv64"; | |
realArgv[1] = "-0"; | |
realArgv[2] = argv[2]; | |
realArgv[3] = fn; | |
memcpy(realArgv + 4, argv + 3, (argc - 3) * sizeof argv[0]); | |
realArgv[argc + 1] = NULL; | |
execv("${pkgs.qemu-riscv}/bin/qemu-riscv64", realArgv); | |
perror("Running qemu-riscv64"); | |
return -1; | |
} | |
''; | |
qemuBinfmtWrapper = pkgs.runCommandCC "qemu-binfmt-wrapper" {} | |
'' | |
mkdir -p $out/bin | |
cc ${qemuBinfmtWrapperSource}/qemu-binfmt-wrapper.c -O3 -o $out/bin/qemu-binfmt-wrapper | |
fixupPhase | |
''; | |
in { boot.binfmtMiscRegistrations.riscv64 = | |
{ magicOrExtension = ''\x7f\x45\x4c\x46\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xf3\x00''; | |
mask = ''\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff''; | |
interpreter = "${qemuBinfmtWrapper}/bin/qemu-binfmt-wrapper"; | |
preserveArgvZero = true; | |
matchCredentials = true; | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment