2 * (C) Copyright 2007 Semihalf
4 * Written by: Rafal Jaworowski <raj@semihalf.com>
6 * See file CREDITS for list of people who contributed to this
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License as
11 * published by the Free Software Foundation; either version 2 of
12 * the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 #include <linux/types.h>
28 #include <api_public.h>
32 static int valid_sig(struct api_signature
*sig
)
35 struct api_signature s
;
40 * Clear the checksum field (in the local copy) so as to calculate the
41 * CRC with the same initial contents as at the time when the sig was
47 checksum
= crc32(0, (unsigned char *)&s
, sizeof(struct api_signature
));
49 if (checksum
!= sig
->checksum
)
56 * Searches for the U-Boot API signature
58 * returns 1/0 depending on found/not found result
60 int api_search_sig(struct api_signature
**sig
) {
67 sp
= (unsigned char *)API_SEARCH_START
;
69 while ((sp
+ (int)API_SIG_MAGLEN
) < (unsigned char *)API_SEARCH_END
) {
70 if (!memcmp(sp
, API_SIG_MAGIC
, API_SIG_MAGLEN
)) {
71 *sig
= (struct api_signature
*)sp
;
82 /****************************************
86 ****************************************/
92 if (!syscall(API_GETC
, NULL
, (uint32_t)&c
))
102 if (!syscall(API_TSTC
, NULL
, (uint32_t)&t
))
110 syscall(API_PUTC
, NULL
, (uint32_t)&c
);
113 void ub_puts(const char *s
)
115 syscall(API_PUTS
, NULL
, (uint32_t)s
);
118 /****************************************
122 ****************************************/
126 syscall(API_RESET
, NULL
);
130 static struct mem_region mr
[MR_MAX
];
131 static struct sys_info si
;
133 struct sys_info
* ub_get_sys_info(void)
137 memset(&si
, 0, sizeof(struct sys_info
));
140 memset(&mr
, 0, sizeof(mr
));
142 if (!syscall(API_GET_SYS_INFO
, &err
, (u_int32_t
)&si
))
145 return ((err
) ? NULL
: &si
);
148 /****************************************
152 ****************************************/
154 void ub_udelay(unsigned long usec
)
156 syscall(API_UDELAY
, NULL
, &usec
);
159 unsigned long ub_get_timer(unsigned long base
)
163 if (!syscall(API_GET_TIMER
, NULL
, &cur
, &base
))
170 /****************************************************************************
174 * Devices are identified by handles: numbers 0, 1, 2, ..., MAX_DEVS-1
176 ***************************************************************************/
180 static struct device_info devices
[MAX_DEVS
];
182 struct device_info
* ub_dev_get(int i
)
184 return ((i
< 0 || i
>= MAX_DEVS
) ? NULL
: &devices
[i
]);
188 * Enumerates the devices: fills out device_info elements in the devices[]
191 * returns: number of devices found
193 int ub_dev_enum(void)
195 struct device_info
*di
;
198 memset(&devices
, 0, sizeof(struct device_info
) * MAX_DEVS
);
201 if (!syscall(API_DEV_ENUM
, NULL
, di
))
204 while (di
->cookie
!= NULL
) {
209 /* take another device_info */
212 /* pass on the previous cookie */
213 di
->cookie
= devices
[n
- 1].cookie
;
215 if (!syscall(API_DEV_ENUM
, NULL
, di
))
223 * handle: 0-based id of the device
225 * returns: 0 when OK, err otherwise
227 int ub_dev_open(int handle
)
229 struct device_info
*di
;
232 if (handle
< 0 || handle
>= MAX_DEVS
)
235 di
= &devices
[handle
];
237 if (!syscall(API_DEV_OPEN
, &err
, di
))
243 int ub_dev_close(int handle
)
245 struct device_info
*di
;
247 if (handle
< 0 || handle
>= MAX_DEVS
)
250 di
= &devices
[handle
];
251 if (!syscall(API_DEV_CLOSE
, NULL
, di
))
259 * Validates device for read/write, it has to:
264 * returns: 0/1 accordingly
266 static int dev_valid(int handle
)
268 if (handle
< 0 || handle
>= MAX_DEVS
)
271 if (devices
[handle
].state
!= DEV_STA_OPEN
)
277 static int dev_stor_valid(int handle
)
279 if (!dev_valid(handle
))
282 if (!(devices
[handle
].type
& DEV_TYP_STOR
))
288 int ub_dev_read(int handle
, void *buf
, lbasize_t len
, lbastart_t start
)
290 struct device_info
*di
;
294 if (!dev_stor_valid(handle
))
297 di
= &devices
[handle
];
298 if (!syscall(API_DEV_READ
, &err
, di
, buf
, &len
, &start
, &act_len
))
310 static int dev_net_valid(int handle
)
312 if (!dev_valid(handle
))
315 if (devices
[handle
].type
!= DEV_TYP_NET
)
321 int ub_dev_recv(int handle
, void *buf
, int len
)
323 struct device_info
*di
;
324 int err
= 0, act_len
;
326 if (!dev_net_valid(handle
))
329 di
= &devices
[handle
];
330 if (!syscall(API_DEV_READ
, &err
, di
, buf
, &len
, &act_len
))
339 int ub_dev_send(int handle
, void *buf
, int len
)
341 struct device_info
*di
;
344 if (!dev_net_valid(handle
))
347 di
= &devices
[handle
];
348 if (!syscall(API_DEV_WRITE
, &err
, di
, buf
, &len
))
354 /****************************************
358 ****************************************/
360 char * ub_env_get(const char *name
)
364 if (!syscall(API_ENV_GET
, NULL
, (uint32_t)name
, (uint32_t)&value
))
370 void ub_env_set(const char *name
, char *value
)
372 syscall(API_ENV_SET
, NULL
, (uint32_t)name
, (uint32_t)value
);
376 static char env_name
[256];
378 const char * ub_env_enum(const char *last
)
380 const char *env
, *str
;
386 * It's OK to pass only the name piece as last (and not the whole
387 * 'name=val' string), since the API_ENUM_ENV call uses envmatch()
388 * internally, which handles such case
390 if (!syscall(API_ENV_ENUM
, NULL
, (uint32_t)last
, (uint32_t)&env
))
394 /* no more env. variables to enumerate */
397 /* next enumerated env var */
398 memset(env_name
, 0, 256);
399 for (i
= 0, str
= env
; *str
!= '=' && *str
!= '\0';)
400 env_name
[i
++] = *str
++;