[fel]rename file.c to misc.c

This commit is contained in:
Jianjun Jiang 2023-07-11 21:54:54 +08:00
parent 3bca4fc658
commit db7f87acf7
4 changed files with 50 additions and 48 deletions

2
fel.h
View File

@ -6,7 +6,7 @@ extern "C" {
#endif
#include <x.h>
#include <file.h>
#include <misc.h>
#include <progress.h>
struct xfel_ctx_t;

43
main.c
View File

@ -5,49 +5,6 @@
#include <spinand.h>
#include <libusb.h>
static inline unsigned char hex_to_bin(char c)
{
if((c >= 'a') && (c <= 'f'))
return c - 'a' + 10;
if((c >= '0') && (c <= '9'))
return c - '0';
if((c >= 'A') && (c <= 'F'))
return c - 'A' + 10;
return 0;
}
static unsigned char hex_string(const char * s, int o)
{
return (hex_to_bin(s[o]) << 4) | hex_to_bin(s[o + 1]);
}
static void hexdump(uint32_t addr, void * buf, size_t len)
{
unsigned char * p = buf;
size_t i, j;
for(j = 0; j < len; j += 16)
{
printf("%08x: ", (uint32_t)(addr + j));
for(i = 0; i < 16; i++)
{
if(j + i < len)
printf("%02x ", p[j + i]);
else
printf(" ");
}
putchar(' ');
for(i = 0; i < 16; i++)
{
if(j + i >= len)
putchar(' ');
else
putchar(isprint(p[j + i]) ? p[j + i] : '.');
}
printf("\r\n");
}
}
static void usage(void)
{
printf("xfel(v1.3.2) - https://github.com/xboot/xfel\r\n");

View File

@ -1,4 +1,4 @@
#include <file.h>
#include <misc.h>
uint64_t file_save(const char * filename, void * buf, uint64_t len)
{
@ -49,3 +49,46 @@ void * file_load(const char * filename, uint64_t * len)
fclose(in);
return buf;
}
static inline unsigned char hex_to_bin(char c)
{
if((c >= 'a') && (c <= 'f'))
return c - 'a' + 10;
if((c >= '0') && (c <= '9'))
return c - '0';
if((c >= 'A') && (c <= 'F'))
return c - 'A' + 10;
return 0;
}
unsigned char hex_string(const char * s, int o)
{
return (hex_to_bin(s[o]) << 4) | hex_to_bin(s[o + 1]);
}
void hexdump(uint32_t addr, void * buf, size_t len)
{
unsigned char * p = buf;
size_t i, j;
for(j = 0; j < len; j += 16)
{
printf("%08x: ", (uint32_t)(addr + j));
for(i = 0; i < 16; i++)
{
if(j + i < len)
printf("%02x ", p[j + i]);
else
printf(" ");
}
putchar(' ');
for(i = 0; i < 16; i++)
{
if(j + i >= len)
putchar(' ');
else
putchar(isprint(p[j + i]) ? p[j + i] : '.');
}
printf("\r\n");
}
}

View File

@ -1,5 +1,5 @@
#ifndef __FILE_H__
#define __FILE_H__
#ifndef __MISC_H__
#define __MISC_H__
#ifdef __cplusplus
extern "C" {
@ -9,9 +9,11 @@ extern "C" {
uint64_t file_save(const char * filename, void * buf, uint64_t len);
void * file_load(const char * filename, uint64_t * len);
unsigned char hex_string(const char * s, int o);
void hexdump(uint32_t addr, void * buf, size_t len);
#ifdef __cplusplus
}
#endif
#endif /* __FILE_H__ */
#endif /* __MISC_H__ */