From 9717fce1860081eb36dec099d26bb59ae07de068 Mon Sep 17 00:00:00 2001
From: MunyDev <jeffplays1292@gmail.com>
Date: Fri, 9 Aug 2024 09:34:40 -0400
Subject: [PATCH] Added some build system

---
 Makefile                               |  3 +++
 make_out.sh                            | 28 ++++++++++++++++++++++++++
 modify.sh                              | 18 +++++++++++++++--
 src/root_store_gen/generate_new_pbs.py |  2 ++
 src/root_store_gen/print_pb.py         |  1 +
 5 files changed, 50 insertions(+), 2 deletions(-)
 create mode 100755 make_out.sh

diff --git a/Makefile b/Makefile
index d4150d5..9ad20d1 100644
--- a/Makefile
+++ b/Makefile
@@ -6,3 +6,6 @@ setup-python:
 	protoc --python_out=gen/python crs.proto
 	cp gen/python/crs_pb2.py src/root_store_gen
 	exit
+build-packed-data:
+	mkdir -p out/PKIMetadata
+	
\ No newline at end of file
diff --git a/make_out.sh b/make_out.sh
new file mode 100755
index 0000000..5f67a13
--- /dev/null
+++ b/make_out.sh
@@ -0,0 +1,28 @@
+#!/bin/bash
+mkdir -p out/PKIMetadata/2000
+SCRIPT_DIR=$(dirname $0)
+if [ $# -lt 1 ]
+then
+    echo "Usage: <root certificates...>"
+    exit 1
+fi
+# Copy all directories, and will be modified by future calls
+rm -rvf "${SCRIPT_DIR}"/out
+mkdir "${SCRIPT_DIR}"/out
+mkdir -p "${SCRIPT_DIR}"/out/PKIMetadata/2000/.
+cp -rvf "${SCRIPT_DIR}"/original/PKIMetadata/2000/. "${SCRIPT_DIR}"/out/PKIMetadata/2000
+rm -rvf "${SCRIPT_DIR}"out/PKIMetadata/2000/_metadata # verified contents not necessary
+python3 ./src/root_store_gen/generate_new_pbs.py "${SCRIPT_DIR}/original/PKIMetadata/2000/crs.pb" "$@" "${SCRIPT_DIR}/out/PKIMetadata/2000/crs.pb"
+# Modify version in manifest
+
+python3 <<EOF
+import json
+from pathlib import Path
+mjs = '${SCRIPT_DIR}/original/PKIMetadata/2000/manifest.json'
+mjs = Path(mjs)
+dat = Path.read_text(mjs)
+print(dat)
+x = json.loads(dat)
+x['version'] = "2000"
+mjs.write_text(json.dumps(x))
+EOF
\ No newline at end of file
diff --git a/modify.sh b/modify.sh
index 42c858f..96d7c1c 100644
--- a/modify.sh
+++ b/modify.sh
@@ -1,6 +1,9 @@
 #!/bin/bash
 
 
+# Copyright 2019 The ChromiumOS Authors
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
 make_block_device_rw() {
   local block_dev="$1"
   [[ -b "${block_dev}" ]] || return 0
@@ -8,6 +11,10 @@ make_block_device_rw() {
     sudo blockdev --setrw "${block_dev}"
   fi
 }
+
+# Copyright 2019 The ChromiumOS Authors
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
 is_ext2_rw_mount_enabled() {
   local rootfs=$1
   local offset="${2-0}"
@@ -18,8 +25,9 @@ is_ext2_rw_mount_enabled() {
   test "${ro_compat_flag}" = "00"
 }
 
-# Returns whether the passed rootfs is an extended filesystem by checking the
-# ext2 s_magic field in the superblock.
+# Copyright 2019 The ChromiumOS Authors
+# Use of this source code is governed by a BSD-style license that can be
+# found in the LICENSE file.
 is_ext_filesystem() {
   local rootfs=$1
   local offset="${2-0}"
@@ -62,6 +70,12 @@ fi
 # Find loop device
 MOUNT_DIR=$(mktemp -d)
 LOOP_DEV=$(losetup -f)
+echo "Script Developer: jeffplays1292@gmail.com"
+
+echo "This tool will modify your shim. There is a chance this tool will render it useless. This tool is in BETA. DO NOT SHARE THIS TOOL! "
+echo "It is recommended to make a backup of your shim before continuing (Press ctrl-c in 5 seconds to stop this tool)"
+sleep 5
+
 losetup -fP "$1"
 echo "Using loop dev at $LOOP_DEV"
 echo "Mounting at $MOUNT_DIR"
diff --git a/src/root_store_gen/generate_new_pbs.py b/src/root_store_gen/generate_new_pbs.py
index d7f9f53..5d72ae1 100644
--- a/src/root_store_gen/generate_new_pbs.py
+++ b/src/root_store_gen/generate_new_pbs.py
@@ -15,11 +15,13 @@ for a in sys.argv[2:-1:]:
     print(f"Registering CA from {a}")
     cas.append(a)
 outfile = sys.argv[-1]
+print(f'reading from: {sys.argv[1]}')
 print(f"Outputing to: {outfile}")
 out = open(outfile, 'wb')
 buf= open(sys.argv[1], 'rb')
 rs = crs_pb2.RootStore()
 rs.ParseFromString(buf.read())
+print(rs.trust_anchors)
 for ca in cas:
     with open(ca, 'rb') as file:
         print(f"Loading Certificate Authority {ca} into rootstore")
diff --git a/src/root_store_gen/print_pb.py b/src/root_store_gen/print_pb.py
index 634305a..beb7968 100644
--- a/src/root_store_gen/print_pb.py
+++ b/src/root_store_gen/print_pb.py
@@ -1,3 +1,4 @@
+#!/bin/python3
 import crs_pb2
 import sys
 if (len(sys.argv) < 2):