Skip to content

Commit 7cc1c07

Browse files
toothbrush7777777phil-opp
authored andcommitted
Pad boot image to block size (#39)
1 parent c2ce530 commit 7cc1c07

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

src/builder.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,37 @@ impl Builder {
297297
});
298298
}
299299

300+
// Pad to nearest block size
301+
{
302+
const BLOCK_SIZE: u64 = 512;
303+
use std::fs::{File, OpenOptions};
304+
let mut file = OpenOptions::new()
305+
.write(true)
306+
.open(&output_bin_path)
307+
.map_err(|err| CreateBootimageError::Io {
308+
message: "failed to open boot image",
309+
error: err,
310+
})?;
311+
let file_size = file
312+
.metadata()
313+
.map_err(|err| CreateBootimageError::Io {
314+
message: "failed to get size of boot image",
315+
error: err,
316+
})?
317+
.len();
318+
let remainder = file_size % BLOCK_SIZE;
319+
let padding = if remainder > 0 {
320+
BLOCK_SIZE - remainder
321+
} else {
322+
0
323+
};
324+
file.set_len(file_size + padding)
325+
.map_err(|err| CreateBootimageError::Io {
326+
message: "failed to pad boot image to a multiple of the block size",
327+
error: err,
328+
})?;
329+
}
330+
300331
Ok(())
301332
}
302333
}

0 commit comments

Comments
 (0)