aarch64: dump device tree blob before length check

In case the final devicetree blob is too large to fit in the memory
reserved for it, it could be useful to inspect the generated FDT. Move
the code that dumps the FDT to a file before the length check so it will
be written out even if the size is too large.

BUG=None
TEST=crosvm run --dump-device-tree-blob /tmp/fdt # on aarch64

Change-Id: I1344d08cee5cb7eb0fd369314f2b765e37f18215
Reviewed-on: https://chromium-review.googlesource.com/c/crosvm/crosvm/+/4277621
Reviewed-by: Keiichi Watanabe <keiichiw@chromium.org>
Commit-Queue: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-by: Shin Kawamura <kawasin@google.com>
diff --git a/aarch64/src/fdt.rs b/aarch64/src/fdt.rs
index 979204c..83450e6 100644
--- a/aarch64/src/fdt.rs
+++ b/aarch64/src/fdt.rs
@@ -593,6 +593,11 @@
 
     let fdt_final = fdt.finish()?;
 
+    if let Some(file_path) = dump_device_tree_blob {
+        std::fs::write(&file_path, &fdt_final)
+            .map_err(|e| Error::FdtDumpIoError(e, file_path.clone()))?;
+    }
+
     if fdt_final.len() > fdt_max_size {
         return Err(Error::TotalSizeTooLarge);
     }
@@ -604,11 +609,6 @@
         return Err(Error::FdtGuestMemoryWriteError);
     }
 
-    if let Some(file_path) = dump_device_tree_blob {
-        std::fs::write(&file_path, &fdt_final)
-            .map_err(|e| Error::FdtDumpIoError(e, file_path.clone()))?;
-    }
-
     Ok(())
 }