Commit 9be5c661beecdb8a9d90ab4869ddb501b4990dd9

Authored by Linus Walleij
Committed by Tom Rini
1 parent 4e1ef15091

arm: semihosting: get rid of forward declarations

By rearranging the functions in the semihosting code we can
avoid forward-declaration of the internal static functions.
This puts the stuff in a logical order: read/open/close/len
and then higher-order functions follow at the end.

Cc: Darwin Rambo <drambo@broadcom.com>
Cc: AKASHI Takahiro <takahiro.akashi@linaro.org>
Cc: Mark Hambleton <mark.hambleton@arm.com>
Cc: Tom Rini <trini@ti.com>
Acked-by: Steve Rae <srae@broadcom.com>
Signed-off-by: Linus Walleij <linus.walleij@linaro.org>

Showing 1 changed file with 84 additions and 89 deletions Side-by-side Diff

arch/arm/lib/semihosting.c
... ... @@ -23,11 +23,6 @@
23 23 #define MODE_READ 0x0
24 24 #define MODE_READBIN 0x1
25 25  
26   -static long smh_read(long fd, void *memp, size_t len);
27   -static long smh_open(const char *fname, char *modestr);
28   -static long smh_close(long fd);
29   -static long smh_len_fd(long fd);
30   -
31 26 /*
32 27 * Call the handler
33 28 */
34 29  
35 30  
36 31  
37 32  
38 33  
39 34  
40 35  
41 36  
42 37  
... ... @@ -44,60 +39,43 @@
44 39 }
45 40  
46 41 /*
47   - * Open, load a file into memory, and close it. Check that the available space
48   - * is sufficient to store the entire file. Return the bytes actually read from
49   - * the file as seen by the read function. The verbose flag enables some extra
50   - * printing of successful read status.
  42 + * Open a file on the host. Mode is "r" or "rb" currently. Returns a file
  43 + * descriptor or -1 on error.
51 44 */
52   -int smh_load(const char *fname, void *memp, int avail, int verbose)
  45 +static long smh_open(const char *fname, char *modestr)
53 46 {
54   - long ret;
55 47 long fd;
56   - size_t len;
  48 + unsigned long mode;
  49 + struct smh_open_s {
  50 + const char *fname;
  51 + unsigned long mode;
  52 + size_t len;
  53 + } open;
57 54  
58   - ret = -1;
  55 + debug("%s: file \'%s\', mode \'%s\'\n", __func__, fname, modestr);
59 56  
60   - debug("%s: fname \'%s\', avail %u, memp %p\n", __func__, fname,
61   - avail, memp);
62   -
63   - /* Open the file */
64   - fd = smh_open(fname, "rb");
65   - if (fd == -1)
  57 + /* Check the file mode */
  58 + if (!(strcmp(modestr, "r"))) {
  59 + mode = MODE_READ;
  60 + } else if (!(strcmp(modestr, "rb"))) {
  61 + mode = MODE_READBIN;
  62 + } else {
  63 + printf("%s: ERROR mode \'%s\' not supported\n", __func__,
  64 + modestr);
66 65 return -1;
67   -
68   - /* Get the file length */
69   - ret = smh_len_fd(fd);
70   - if (ret == -1) {
71   - smh_close(fd);
72   - return -1;
73 66 }
74 67  
75   - /* Check that the file will fit in the supplied buffer */
76   - if (ret > avail) {
77   - printf("%s: ERROR ret %ld, avail %u\n", __func__, ret,
78   - avail);
79   - smh_close(fd);
80   - return -1;
81   - }
  68 + open.fname = fname;
  69 + open.len = strlen(fname);
  70 + open.mode = mode;
82 71  
83   - len = ret;
  72 + /* Open the file on the host */
  73 + fd = smh_trap(SYSOPEN, &open);
  74 + if (fd == -1)
  75 + printf("%s: ERROR fd %ld for file \'%s\'\n", __func__, fd,
  76 + fname);
84 77  
85   - /* Read the file into the buffer */
86   - ret = smh_read(fd, memp, len);
87   - if (ret == 0) {
88   - /* Print successful load information if requested */
89   - if (verbose) {
90   - printf("\n%s\n", fname);
91   - printf(" 0x%8p dest\n", memp);
92   - printf(" 0x%08lx size\n", len);
93   - printf(" 0x%08x avail\n", avail);
94   - }
95   - }
96   -
97   - /* Close the file */
98   - smh_close(fd);
99   -
100   - return ret;
  78 + return fd;
101 79 }
102 80  
103 81 /*
... ... @@ -135,46 +113,6 @@
135 113 }
136 114  
137 115 /*
138   - * Open a file on the host. Mode is "r" or "rb" currently. Returns a file
139   - * descriptor or -1 on error.
140   - */
141   -static long smh_open(const char *fname, char *modestr)
142   -{
143   - long fd;
144   - unsigned long mode;
145   - struct smh_open_s {
146   - const char *fname;
147   - unsigned long mode;
148   - size_t len;
149   - } open;
150   -
151   - debug("%s: file \'%s\', mode \'%s\'\n", __func__, fname, modestr);
152   -
153   - /* Check the file mode */
154   - if (!(strcmp(modestr, "r"))) {
155   - mode = MODE_READ;
156   - } else if (!(strcmp(modestr, "rb"))) {
157   - mode = MODE_READBIN;
158   - } else {
159   - printf("%s: ERROR mode \'%s\' not supported\n", __func__,
160   - modestr);
161   - return -1;
162   - }
163   -
164   - open.fname = fname;
165   - open.len = strlen(fname);
166   - open.mode = mode;
167   -
168   - /* Open the file on the host */
169   - fd = smh_trap(SYSOPEN, &open);
170   - if (fd == -1)
171   - printf("%s: ERROR fd %ld for file \'%s\'\n", __func__, fd,
172   - fname);
173   -
174   - return fd;
175   -}
176   -
177   -/*
178 116 * Close the file using the file descriptor
179 117 */
180 118 static long smh_close(long fd)
... ... @@ -202,6 +140,63 @@
202 140 ret = smh_trap(SYSFLEN, &fd);
203 141 if (ret == -1)
204 142 printf("%s: ERROR ret %ld, fd %ld\n", __func__, ret, fd);
  143 +
  144 + return ret;
  145 +}
  146 +
  147 +/*
  148 + * Open, load a file into memory, and close it. Check that the available space
  149 + * is sufficient to store the entire file. Return the bytes actually read from
  150 + * the file as seen by the read function. The verbose flag enables some extra
  151 + * printing of successful read status.
  152 + */
  153 +int smh_load(const char *fname, void *memp, int avail, int verbose)
  154 +{
  155 + long ret;
  156 + long fd;
  157 + size_t len;
  158 +
  159 + ret = -1;
  160 +
  161 + debug("%s: fname \'%s\', avail %u, memp %p\n", __func__, fname,
  162 + avail, memp);
  163 +
  164 + /* Open the file */
  165 + fd = smh_open(fname, "rb");
  166 + if (fd == -1)
  167 + return -1;
  168 +
  169 + /* Get the file length */
  170 + ret = smh_len_fd(fd);
  171 + if (ret == -1) {
  172 + smh_close(fd);
  173 + return -1;
  174 + }
  175 +
  176 + /* Check that the file will fit in the supplied buffer */
  177 + if (ret > avail) {
  178 + printf("%s: ERROR ret %ld, avail %u\n", __func__, ret,
  179 + avail);
  180 + smh_close(fd);
  181 + return -1;
  182 + }
  183 +
  184 + len = ret;
  185 +
  186 + /* Read the file into the buffer */
  187 + ret = smh_read(fd, memp, len);
  188 + if (ret == 0) {
  189 + /* Print successful load information if requested */
  190 + if (verbose) {
  191 + printf("\n%s\n", fname);
  192 + printf(" 0x%8p dest\n", memp);
  193 + printf(" 0x%08lx size\n", len);
  194 + printf(" 0x%08x avail\n", avail);
  195 + }
  196 + }
  197 +
  198 + /* Close the file */
  199 + smh_close(fd);
205 200  
206 201 return ret;
207 202 }