@@ -2040,6 +2040,158 @@ void test_loader_cmd_enable_crc_mismatch(void) {
20402040 TEST_ASSERT (mock_output_contains ("CRC mismatch" ));
20412041}
20422042
2043+ /* ============================================================================
2044+ * Cache Flush / ICache Invalidate Tests
2045+ * ============================================================================ */
2046+
2047+ static void setup_loader_with_cache (void ) {
2048+ setup_loader ();
2049+ test_ctx .flush_dcache_cb = mock_flush_dcache ;
2050+ test_ctx .invalidate_icache_cb = mock_invalidate_icache ;
2051+ }
2052+
2053+ void test_loader_upload_flushes_dcache (void ) {
2054+ setup_loader_with_cache ();
2055+ fl_init (& test_ctx );
2056+
2057+ const char * alloc_argv [] = {"fl" , "--cmd" , "alloc" , "--size" , "64" };
2058+ fl_exec_cmd (& test_ctx , 5 , alloc_argv );
2059+
2060+ mock_reset_call_stats ();
2061+
2062+ const char * argv [] = {"fl" , "--cmd" , "upload" , "--addr" , "0" , "--data" , "AQIDBA==" };
2063+ fl_error_t result = fl_exec_cmd (& test_ctx , 7 , argv );
2064+
2065+ TEST_ASSERT_EQUAL (FL_OK , result );
2066+ const mock_call_stats_t * stats = mock_get_call_stats ();
2067+ TEST_ASSERT (stats -> flush_dcache_count >= 1 );
2068+ TEST_ASSERT (stats -> last_dcache_start != 0 );
2069+ TEST_ASSERT (stats -> last_dcache_end > stats -> last_dcache_start );
2070+ }
2071+
2072+ void test_loader_upload_invalidates_icache (void ) {
2073+ setup_loader_with_cache ();
2074+ fl_init (& test_ctx );
2075+
2076+ const char * alloc_argv [] = {"fl" , "--cmd" , "alloc" , "--size" , "64" };
2077+ fl_exec_cmd (& test_ctx , 5 , alloc_argv );
2078+
2079+ mock_reset_call_stats ();
2080+
2081+ const char * argv [] = {"fl" , "--cmd" , "upload" , "--addr" , "0" , "--data" , "AQIDBA==" };
2082+ fl_error_t result = fl_exec_cmd (& test_ctx , 7 , argv );
2083+
2084+ TEST_ASSERT_EQUAL (FL_OK , result );
2085+ const mock_call_stats_t * stats = mock_get_call_stats ();
2086+ TEST_ASSERT (stats -> invalidate_icache_count >= 1 );
2087+ TEST_ASSERT (stats -> last_icache_start != 0 );
2088+ TEST_ASSERT (stats -> last_icache_end > stats -> last_icache_start );
2089+ }
2090+
2091+ void test_loader_upload_cache_range_matches (void ) {
2092+ setup_loader_with_cache ();
2093+ fl_init (& test_ctx );
2094+
2095+ const char * alloc_argv [] = {"fl" , "--cmd" , "alloc" , "--size" , "64" };
2096+ fl_exec_cmd (& test_ctx , 5 , alloc_argv );
2097+
2098+ uintptr_t alloc_addr = test_ctx .last_alloc ;
2099+ mock_reset_call_stats ();
2100+
2101+ /* Upload 4 bytes at offset 0 */
2102+ const char * argv [] = {"fl" , "--cmd" , "upload" , "--addr" , "0" , "--data" , "AQIDBA==" };
2103+ fl_exec_cmd (& test_ctx , 7 , argv );
2104+
2105+ const mock_call_stats_t * stats = mock_get_call_stats ();
2106+ /* dcache and icache should cover the same range */
2107+ TEST_ASSERT_EQUAL_HEX (alloc_addr , stats -> last_dcache_start );
2108+ TEST_ASSERT_EQUAL_HEX (alloc_addr + 4 , stats -> last_dcache_end );
2109+ TEST_ASSERT_EQUAL_HEX (alloc_addr , stats -> last_icache_start );
2110+ TEST_ASSERT_EQUAL_HEX (alloc_addr + 4 , stats -> last_icache_end );
2111+ }
2112+
2113+ void test_loader_write_flushes_dcache (void ) {
2114+ setup_loader_with_cache ();
2115+ fl_init (& test_ctx );
2116+
2117+ const char * alloc_argv [] = {"fl" , "--cmd" , "alloc" , "--size" , "64" };
2118+ fl_exec_cmd (& test_ctx , 5 , alloc_argv );
2119+
2120+ uintptr_t alloc_addr = test_ctx .last_alloc ;
2121+ char addr_str [32 ];
2122+ snprintf (addr_str , sizeof (addr_str ), "0x%lX" , (unsigned long )alloc_addr );
2123+
2124+ mock_reset_call_stats ();
2125+
2126+ const char * argv [] = {"fl" , "--cmd" , "write" , "--addr" , addr_str , "--data" , "AQIDBA==" };
2127+ fl_error_t result = fl_exec_cmd (& test_ctx , 7 , argv );
2128+
2129+ TEST_ASSERT_EQUAL (FL_OK , result );
2130+ const mock_call_stats_t * stats = mock_get_call_stats ();
2131+ TEST_ASSERT (stats -> flush_dcache_count >= 1 );
2132+ }
2133+
2134+ void test_loader_write_invalidates_icache (void ) {
2135+ setup_loader_with_cache ();
2136+ fl_init (& test_ctx );
2137+
2138+ const char * alloc_argv [] = {"fl" , "--cmd" , "alloc" , "--size" , "64" };
2139+ fl_exec_cmd (& test_ctx , 5 , alloc_argv );
2140+
2141+ uintptr_t alloc_addr = test_ctx .last_alloc ;
2142+ char addr_str [32 ];
2143+ snprintf (addr_str , sizeof (addr_str ), "0x%lX" , (unsigned long )alloc_addr );
2144+
2145+ mock_reset_call_stats ();
2146+
2147+ const char * argv [] = {"fl" , "--cmd" , "write" , "--addr" , addr_str , "--data" , "AQIDBA==" };
2148+ fl_error_t result = fl_exec_cmd (& test_ctx , 7 , argv );
2149+
2150+ TEST_ASSERT_EQUAL (FL_OK , result );
2151+ const mock_call_stats_t * stats = mock_get_call_stats ();
2152+ TEST_ASSERT (stats -> invalidate_icache_count >= 1 );
2153+ }
2154+
2155+ void test_loader_no_cache_cb_no_crash (void ) {
2156+ /* Without cache callbacks set, upload/write should still work */
2157+ setup_loader ();
2158+ fl_init (& test_ctx );
2159+
2160+ /* Ensure no cache callbacks */
2161+ TEST_ASSERT (test_ctx .flush_dcache_cb == NULL );
2162+ TEST_ASSERT (test_ctx .invalidate_icache_cb == NULL );
2163+
2164+ const char * alloc_argv [] = {"fl" , "--cmd" , "alloc" , "--size" , "64" };
2165+ fl_exec_cmd (& test_ctx , 5 , alloc_argv );
2166+
2167+ const char * argv [] = {"fl" , "--cmd" , "upload" , "--addr" , "0" , "--data" , "AQIDBA==" };
2168+ fl_error_t result = fl_exec_cmd (& test_ctx , 7 , argv );
2169+
2170+ TEST_ASSERT_EQUAL (FL_OK , result );
2171+ }
2172+
2173+ void test_loader_upload_offset_cache_range (void ) {
2174+ setup_loader_with_cache ();
2175+ fl_init (& test_ctx );
2176+
2177+ const char * alloc_argv [] = {"fl" , "--cmd" , "alloc" , "--size" , "128" };
2178+ fl_exec_cmd (& test_ctx , 5 , alloc_argv );
2179+
2180+ uintptr_t alloc_addr = test_ctx .last_alloc ;
2181+ mock_reset_call_stats ();
2182+
2183+ /* Upload 4 bytes at offset 16 */
2184+ const char * argv [] = {"fl" , "--cmd" , "upload" , "--addr" , "16" , "--data" , "AQIDBA==" };
2185+ fl_exec_cmd (& test_ctx , 7 , argv );
2186+
2187+ const mock_call_stats_t * stats = mock_get_call_stats ();
2188+ /* Cache range should be alloc_addr+16 to alloc_addr+20 */
2189+ TEST_ASSERT_EQUAL_HEX (alloc_addr + 16 , stats -> last_dcache_start );
2190+ TEST_ASSERT_EQUAL_HEX (alloc_addr + 20 , stats -> last_dcache_end );
2191+ TEST_ASSERT_EQUAL_HEX (alloc_addr + 16 , stats -> last_icache_start );
2192+ TEST_ASSERT_EQUAL_HEX (alloc_addr + 20 , stats -> last_icache_end );
2193+ }
2194+
20432195/* ============================================================================
20442196 * Test Runner
20452197 * ============================================================================ */
@@ -2196,4 +2348,14 @@ void run_loader_tests(void) {
21962348 RUN_TEST (test_loader_cmd_enable_with_crc );
21972349 RUN_TEST (test_loader_cmd_enable_crc_mismatch );
21982350 TEST_SUITE_END ();
2351+
2352+ TEST_SUITE_BEGIN ("func_loader - Cache Flush / ICache Invalidate" );
2353+ RUN_TEST (test_loader_upload_flushes_dcache );
2354+ RUN_TEST (test_loader_upload_invalidates_icache );
2355+ RUN_TEST (test_loader_upload_cache_range_matches );
2356+ RUN_TEST (test_loader_write_flushes_dcache );
2357+ RUN_TEST (test_loader_write_invalidates_icache );
2358+ RUN_TEST (test_loader_no_cache_cb_no_crash );
2359+ RUN_TEST (test_loader_upload_offset_cache_range );
2360+ TEST_SUITE_END ();
21992361}
0 commit comments