summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Sandberg <andreas.sandberg@arm.com>2017-04-26 16:46:13 +0000
committerAndreas Sandberg <andreas.sandberg@arm.com>2017-05-09 09:24:06 +0000
commit708258aba60cfeb092b11c2aa6bb867dce233d47 (patch)
tree5a3ff110076abda388b61c7d5993d86bac353d03
parenta59d153c08f05e5bfdb2689c20329a75abf092b4 (diff)
downloadgem5-708258aba60cfeb092b11c2aa6bb867dce233d47.tar.xz
util: Correctly handle short writes in m5 (read|exec)file
The m5 tool has subcommands that writes a file to the simulated file system. The implementation of this command currently doesn't check the return value from write, which leads to compiler warnings and potentially incorrect behavior. Add the necessary checks. Change-Id: If558534d3245aa24cf15edf06bd0af4c6ba3908c Signed-off-by: Andreas Sandberg <andreas.sandberg@arm.com> Reviewed-by: Nikos Nikoleris <nikos.nikoleris@arm.com> Reviewed-on: https://gem5-review.googlesource.com/2962 Reviewed-by: Jason Lowe-Power <jason@lowepower.com> Reviewed-by: Gabe Black <gabeblack@google.com>
-rw-r--r--util/m5/m5.c24
1 files changed, 20 insertions, 4 deletions
diff --git a/util/m5/m5.c b/util/m5/m5.c
index 8e21276b0..d39098c68 100644
--- a/util/m5/m5.c
+++ b/util/m5/m5.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2011 ARM Limited
+ * Copyright (c) 2011, 2017 ARM Limited
* All rights reserved
*
* The license below extends only to copyright in the software and shall
@@ -107,9 +107,9 @@ parse_str_args_to_regs(int argc, char *argv[], uint64_t regs[], int len)
int
read_file(int dest_fid)
{
- char buf[256*1024];
+ uint8_t buf[256*1024];
int offset = 0;
- int len;
+ int len, ret;
// Touch all buffer pages to ensure they are mapped in the
// page table. This is required in the case of X86_FS, where
@@ -117,9 +117,25 @@ read_file(int dest_fid)
memset(buf, 0, sizeof(buf));
while ((len = m5_readfile(buf, sizeof(buf), offset)) > 0) {
- write(dest_fid, buf, len);
+ uint8_t *base = buf;
offset += len;
+ do {
+ ret = write(dest_fid, base, len);
+ if (ret < 0) {
+ perror("Failed to write file");
+ exit(2);
+ } else if (ret == 0) {
+ fprintf(stderr, "Failed to write file: "
+ "Unhandled short write\n");
+ exit(2);
+ }
+
+ base += ret;
+ len -= ret;
+ } while (len);
}
+
+ return offset;
}
void