elfloader/shoehorn: cleanup sel4test fudge factor hack

elfloader uses shoehorn to find a memory location to load build
artifacts. There was a hack to artificially increase the required space
by 4MiB for sel4test. This breaks systems with limited memory so move
this hack to a shoehorn command line option and add cmake glue to enable
it to be set in the sel4test build glue.

Bug: 197745020

Change-Id: I146976ae23cd4fcdd7a422d9c1643428e3596efb
diff --git a/cmake-tool/helpers/shoehorn.py b/cmake-tool/helpers/shoehorn.py
index 0bba19b..e6dcee0 100755
--- a/cmake-tool/helpers/shoehorn.py
+++ b/cmake-tool/helpers/shoehorn.py
@@ -126,6 +126,11 @@
                         default=False, action='store_true',
                         help='assume ELF-loader will put rootservers at top of'
                              ' memory')
+    parser.add_argument('--fudge-factor',
+                        dest='fudge_factor',
+                        type=int,
+                        nargs='?',
+                        help='Extra space to make room for (bytes)')
     parser.add_argument('platform_filename', nargs=1, type=str,
                         help='YAML description of platform parameters (e.g.,'
                              ' platform_gen.yaml)')
@@ -138,6 +143,7 @@
     image_size = os.path.getsize(image)
     do_load_rootservers_high = args.load_rootservers_high
     platform = platform_sift.load_data(args.platform_filename[0])
+    fudge_factor = args.fudge_factor
 
     rootservers = []
     is_dtb_present = False
@@ -202,11 +208,12 @@
                 marker += elf_sift.get_memory_usage(elf, align=True)
                 debug_marker_set(marker, 'end of rootserver')
 
-            # Note: sel4test_driver eats (almost) 4 more MiB than it claims to.
-            # Fixing this is JIRA SELFOUR-2335.
-            fudge_factor = 4 * 1024 * 1024
-            marker += elf_sift.get_aligned_size(fudge_factor)
-            debug_marker_set(marker, 'end of (aligned) fudge factor')
+            if fudge_factor:
+                # Note: sel4test_driver eats (almost) 4 more MiB than it claims to.
+                # Fixing this is JIRA SELFOUR-2335.
+                # fudge_factor = 4 * 1024 * 1024
+                marker += elf_sift.get_aligned_size(fudge_factor)
+                debug_marker_set(marker, 'end of (aligned) fudge factor')
 
         image_start_address = marker
 
diff --git a/elfloader-tool/CMakeLists.txt b/elfloader-tool/CMakeLists.txt
index 8ee1244..e83987d 100644
--- a/elfloader-tool/CMakeLists.txt
+++ b/elfloader-tool/CMakeLists.txt
@@ -91,6 +91,12 @@
     DEPENDS KernelArchArmV8a
 )
 
+config_option(
+    ElfLoaderFudgeFactor ELFLOADER_FUDGE_FACTOR
+    "Additional space for shoehorn to reserve"
+    DEFAULT OFF
+)
+
 add_config_library(elfloader "${configure_string}")
 
 add_compile_options(-D_XOPEN_SOURCE=700 -ffreestanding -Wall -Werror -W -Wextra)
@@ -294,6 +300,9 @@
     set(PLATFORM_SIFT "${CMAKE_TOOL_HELPERS_DIR}/platform_sift.py")
     set(ELF_SIFT "${CMAKE_TOOL_HELPERS_DIR}/elf_sift.py")
     set(SHOEHORN "${CMAKE_TOOL_HELPERS_DIR}/shoehorn.py")
+    if(ElfloaderFudgeFactor)
+        set(SHOEHORN_OPTIONS "--fudge-factor=${ElfloaderFudgeFactor}")
+    endif()
     set(ARCHIVE_O "${CMAKE_CURRENT_BINARY_DIR}/archive.o")
     add_custom_command(
         OUTPUT "${IMAGE_START_ADDR_H}" "${PLATFORM_INFO_H}"
@@ -309,7 +318,7 @@
             # The `shoehorn` tool computes a reasonable image start address. It calls
             # `elf_sift` to obtain details about where the extracted payloads will be
             # and how big they are.
-            "${SHOEHORN}" "${platform_yaml}" "${ARCHIVE_O}" > "${IMAGE_START_ADDR_H}"
+            "${SHOEHORN}" ${SHOEHORN_OPTIONS} "${platform_yaml}" "${ARCHIVE_O}" > "${IMAGE_START_ADDR_H}"
         VERBATIM
         DEPENDS
             # First command's dependencies
diff --git a/elfloader-tool/helpers.cmake b/elfloader-tool/helpers.cmake
index ec97df5..ce1b2ae 100644
--- a/elfloader-tool/helpers.cmake
+++ b/elfloader-tool/helpers.cmake
@@ -11,3 +11,8 @@
 function(SetElfloaderRootserversLast)
     set(ElfloaderRootserversLast ON CACHE BOOL "" FORCE)
 endfunction()
+
+# Hook for sel4test to effect shoehorn work.
+function(SetElfloaderFudgeFactor FUDGE)
+    set(ElfloaderFudgeFactor ${FUDGE} CACHE STRING "" FORCE)
+endfunction()