From 6892c2c6a5c9323adfe23bce4f0cb3f47d6afbc4 Mon Sep 17 00:00:00 2001 From: xwashere Date: Fri, 1 Mar 2024 13:16:18 -0500 Subject: [PATCH] manually create nt symlinks --- CMakeLists.txt | 2 +- README | 3 +++ lsmss/main.cxx | 50 +++++++++++++++++++++++++++++++++++++---------- scripts/copylib | 22 +++++++++++++++++++-- src/grub/grub.cfg | 2 +- 5 files changed, 65 insertions(+), 14 deletions(-) create mode 100644 README diff --git a/CMakeLists.txt b/CMakeLists.txt index d710643..0378ee3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -274,7 +274,7 @@ add_custom_command(OUTPUT lindows_c.img COMMAND ${CoreUtils_Mkdir_EXECUTABLE} ARGS lindows_c/Windows/System32/LNXConfig COMMAND ${CoreUtils_Mkdir_EXECUTABLE} ARGS lindows_c/Windows/GRUB COMMAND ${CoreUtils_Mkdir_EXECUTABLE} ARGS lindows_c/Windows/GRUB/x86_64-efi - + COMMAND ${CoreUtils_Mkdir_EXECUTABLE} ARGS 'lindows_c/Windows/Temporary Files' # grub stuff COMMAND ${CoreUtils_Copy_EXECUTABLE} ARGS ${CMAKE_CURRENT_BINARY_DIR}/grub/lib/grub/x86_64-efi/* lindows_c/Windows/GRUB/x86_64-efi COMMAND ${CoreUtils_Copy_EXECUTABLE} ARGS ${CMAKE_CURRENT_SOURCE_DIR}/src/grub/grub.cfg lindows_c/Windows/GRUB/grub.cfg diff --git a/README b/README new file mode 100644 index 0000000..41e8fab --- /dev/null +++ b/README @@ -0,0 +1,3 @@ +== NAMING == + +Pii: Preinit internal core routines diff --git a/lsmss/main.cxx b/lsmss/main.cxx index 812707d..3e8eafa 100644 --- a/lsmss/main.cxx +++ b/lsmss/main.cxx @@ -2,10 +2,13 @@ #include #include #include +#include #include +#include #include +#include /** * execute a program from an absolute path @@ -13,7 +16,7 @@ * \param path absolute path to program (duh) * \return program return value, 1 if it does not exist or -1 on error */ -int execute(const std::string& path) { +int Pii_ExecuteProgram(const std::string& path) { if (pid_t child = fork()) { int exitcode = -1; waitpid(child, &exitcode, 0); @@ -28,21 +31,48 @@ int execute(const std::string& path) { } } +/** + * Abort the boot process and drop the user into a recovery shell + */ +[[noreturn]] +void Pii_AbortBoot() { + std::cout << "Unable to complete preinitialization. You have been dropped into a minimal recovery environment. Good luck." << std::endl; + Pii_ExecuteProgram("/Windows/System32/bash.exe"); + std::exit(1); +} + int main() { - bool ok = true; + // create temporary files + std::cout << "Mounting temporary filesystems\n"; + if (mount("tmpfs", "/Windows/Temporary Files/", "tmpfs", 0, "") < 0) { + perror("mount"); + Pii_AbortBoot(); + } + + // we need this because we mount as readonly in preinit so that we can prep the system + // and load the user mode ntfs driver + // std::cout << "Creating system32 overlay for preinit\n"; + // std::filesystem::create_directory("/Windows/Temporary Files/System32OverlayUD"); + // std::filesystem::create_directory("/Windows/Temporary Files/System32OverlayWD"); + // if (mount("overlay", "/Windows/System32/", "overlay", 0, "lowerdir=/Windows/System32,upperdir=/Windows/Temporary Files/System32OverlayUD,workdir=/Windoes/Temporary Files/System32OverlayWD") < 0) { + // perror("mount"); + // Pii_AbortBoot(); + // } + + // oh wow ! + // std::cout << "Symlimking well known library names\n"; // dynamic linking fun std::cout << "Regenerating dynamic linker cache\n"; - // ok = execute("/Windows/System32/ldconfig.exe") == 0; + // Pii_ExecuteProgram("/Windows/System32/ldconfig.exe"); - // we are never ok - ok = false; - - // failure :> - if (!ok) { - std::cout << "Unable to complete preinitialization. You have been dropped into a minimal recovery environment. Good luck" << std::endl; - execute("/Windows/System32/bash.exe"); + // boot still failed + for (const auto& ent : std::filesystem::directory_iterator("/Windows/System32")) { + std::cout << ent << " s:" << ent.is_symlink() << " d:" << ent.is_directory() << "\n"; } + // we are never ok + Pii_AbortBoot(); + return 0; } diff --git a/scripts/copylib b/scripts/copylib index d0a3a74..104820d 100755 --- a/scripts/copylib +++ b/scripts/copylib @@ -6,7 +6,25 @@ find $1/bin $1/lib -type d | sed -r 's/'$1'\/(bin|lib)\///;Tn;s/^/mkdir lindows_ find $1/bin -type f | sed -r 's/'$1'\/bin\/(.*)/cp '$1'\/bin\/\1 lindows_c\/Windows\/System32\/\1.exe/;e' > /dev/null find $1/lib -type f | sed -r 's/'$1'\/lib\/(.*)/cp '$1'\/lib\/\1 lindows_c\/Windows\/System32\/\1/;e' > /dev/null -# copy lib links +# ntfs-3g cant seem to figure out ntfs symlinks, its 1500 lines of code for reparse points dont even work +# with symlinks, so we have to build the reparse point and do it ourselves for lib in $(cd $1/lib && find -type l); do - ln -sr "lindows_c/Windows/System32/$(readlink $1/lib/$lib)" "lindows_c/Windows/System32/$lib" + WPATH="$(echo -n $(readlink "$1/lib/$lib"))" + LPATH="$(echo -n '/Windows/System32/'"$WPATH" | sed -E 's/(.)/\1\\x00/g;s/\//\\\\/g')" # sed -E 's/(.)/\\0\1/g'); + LPATHL=$(( 36 + ${#WPATH} * 2 )); + + LPATHL1=$(printf "%o" $((($LPATHL >> 8 ) & 255 )) ); + LPATHL0=$(printf "%o" $((($LPATHL ) & 255 )) ); + + LBUF="\\x00\\x00\\$LPATHL0\\$LPATHL1\\x00\\x00\\$LPATHL0\\$LPATHL1\\x00\\x00\\x00\\x00$LPATH" + LBUFL=$(( 12 + $LPATHL )) + LBUFL1=$(printf "%o" $((($LBUFL >> 8) & 255 )) ); + LBUFL0=$(printf "%o" $((($LBUFL ) & 255 )) ); + + touch "lindows_c/Windows/System32/$lib"; + setfattr -h -n system.ntfs_reparse_data \ + -v "0S$(printf "\x0C\\x00\\x00\xA0\\$LBUFL0\\$LBUFL1\\x00\\x00$LBUF" | base64 -w0)"\ + "lindows_c/Windows/System32/$lib"; + + #ln -sr "lindows_c/Windows/System32/$(readlink $1/lib/$lib)" "lindows_c/Windows/System32/$lib" done diff --git a/src/grub/grub.cfg b/src/grub/grub.cfg index 32579ce..0a2f2a6 100644 --- a/src/grub/grub.cfg +++ b/src/grub/grub.cfg @@ -2,5 +2,5 @@ clear # boot -linux /Windows/vmlinux.exe console=tty0 console=ttyS0 root=PARTUUID=48d29da8-2ff8-4f23-ba1a-0e8ccfc329e2 rw init=/Windows/System32/lsmss.exe +linux /Windows/vmlinux.exe console=tty0 console=ttyS0 root=PARTUUID=48d29da8-2ff8-4f23-ba1a-0e8ccfc329e2 init=/Windows/System32/lsmss.exe boot