commit 0a919a418ee333967f6009fa15c06f55ac0be44b
parent b961efc6c5e1e38cf8bd15465311bb9bc1f559e3
Author: Ambroise Vincent <ambroise.vincent@arm.com>
Date: Mon, 13 May 2019 13:23:45 +0100
Merge changes from topic 'av/load'
* changes:
[rom] Add upper bound parameter to loadimg()
[rom] Add loadimg()
[rom] Make use of dirstat()
Diffstat:
1 file changed, 46 insertions(+), 16 deletions(-)
diff --git a/target/native/rom.c b/target/native/rom.c
@@ -3,6 +3,7 @@
#include <string.h>
#include <libk.h>
+#include <limits.h>
#include <rcode/rcode.h>
#include <rcode/romfw.h>
@@ -153,34 +154,63 @@ error:
panic("namespace");
}
-void
-loadfip(void)
+// TODO: move to generic code
+int
+loadimg(char *name, void *dst, long *n)
{
int fd;
+ Dir dir;
+ long cnt, r;
+ char *d = dst;
+
+ if (dirstat(name, &dir) < 0)
+ goto err0;
+ fd = open(name, O_READ);
+ if (fd < 0)
+ goto err0;
+
+ if (n) {
+ if (dir.length > *n && *n > 0)
+ dir.length = *n;
+ }
- fd = open("/fip/bl2.bin", O_READ);
- if (fd < 0) {
- kerror("bl2 missing");
- goto error;
+ for (cnt = dir.length; cnt > 0; cnt -= r) {
+ r = (cnt > INT_MAX) ? INT_MAX : cnt;
+ r = read(fd, d, r);
+ if (r == 0)
+ break;
+ if (r < 0)
+ goto err1;
+ d += r;
}
- read(fd, (void *) 0x4022000, 0x80000);
+
+ if (n)
+ *n = dir.length - cnt;
+
close(fd);
+ return 0;
+
+err1:
+ close(fd);
+err0:
+ return -1;
+}
- fd = open("/fip/bl33.bin", O_READ);
- if (fd < 0) {
+void
+loadfip(void)
+{
+ if (loadimg("/fip/bl2.bin", (void *) 0x4022000, NULL) < 0) {
+ kerror("bl2 missing");
+ goto error;
+ }
+ if (loadimg("/fip/bl33.bin", (void *) 0x88000000, NULL) < 0) {
kerror("bl33 missing");
goto error;
}
- read(fd, (void *) 0x88000000, 0x80000);
- close(fd);
-
- fd = open("/fip/hw.cfg", O_READ);
- if (fd < 0) {
+ if (loadimg("/fip/hw.cfg", (void *) 0x82000000, NULL) < 0) {
kerror("dtb missing");
goto error;
}
- read(fd, (void *) 0x82000000, 0x80000);
- close(fd);
return;