2023-06-01 07:31:08 +00:00
|
|
|
/* Copyright (c) 2022, 2023 Leah Rowe <info@minifree.org> */
|
|
|
|
/* SPDX-License-Identifier: MIT */
|
2023-06-01 07:25:55 +00:00
|
|
|
|
2023-06-01 07:47:54 +00:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
2023-06-01 12:35:34 +00:00
|
|
|
void openFiles(const char *path);
|
|
|
|
void readGbeFile(const char *path);
|
2023-06-01 07:25:55 +00:00
|
|
|
void cmd_setmac(const char *strMac);
|
|
|
|
int invalidMacAddress(const char *strMac, uint16_t *mac);
|
|
|
|
uint8_t hextonum(char chs);
|
|
|
|
uint8_t rhex(void);
|
|
|
|
void cmd_dump(void);
|
|
|
|
void showmac(int partnum);
|
|
|
|
void hexdump(int partnum);
|
|
|
|
void cmd_setchecksum(void);
|
|
|
|
void cmd_brick(void);
|
|
|
|
void cmd_swap(void);
|
|
|
|
void cmd_copy(void);
|
|
|
|
int validChecksum(int partnum);
|
|
|
|
void setWord(int pos16, int partnum, uint16_t val16);
|
2023-06-01 09:31:21 +00:00
|
|
|
void xorswap_buf(int partnum);
|
|
|
|
void writeGbeFile(const char *filename);
|
2023-06-01 07:25:55 +00:00
|
|
|
|
|
|
|
#define FILENAME argv[1]
|
|
|
|
#define COMMAND argv[2]
|
|
|
|
#define MAC_ADDRESS argv[3]
|
|
|
|
#define PARTNUM argv[3]
|
|
|
|
#define SIZE_4KB 0x1000
|
|
|
|
#define SIZE_8KB 0x2000
|
|
|
|
|
|
|
|
uint16_t buf16[SIZE_4KB];
|
2023-06-01 09:31:21 +00:00
|
|
|
uint8_t *buf = (uint8_t *) &buf16;
|
|
|
|
size_t nf = 128, gbe[2];
|
2023-06-01 07:25:55 +00:00
|
|
|
uint8_t skipread[2] = {0, 0};
|
|
|
|
|
2023-06-01 13:04:44 +00:00
|
|
|
int flags = O_RDWR, rfd, fd, part, gbeFileModified = 0;
|
2023-06-01 07:25:55 +00:00
|
|
|
uint8_t nvmPartModified[2] = {0, 0};
|
|
|
|
|
2023-06-01 09:31:21 +00:00
|
|
|
int test = 1;
|
|
|
|
int big_endian;
|
2023-06-01 07:25:55 +00:00
|
|
|
|
|
|
|
#define ERR() errno = errno ? errno : ECANCELED
|
|
|
|
#define err_if(x) if (x) err(ERR(), NULL)
|
|
|
|
|
2023-06-01 11:58:33 +00:00
|
|
|
#define xopen(f,l,p) if (opendir(l) != NULL) err(errno = EISDIR, "%s", l); \
|
2023-06-01 10:56:01 +00:00
|
|
|
if ((f = open(l, p)) == -1) err(ERR(), "%s", l); \
|
2023-06-01 13:04:44 +00:00
|
|
|
if (fstat(f, &st) == -1) err(ERR(), "%s", l)
|
2023-06-01 09:31:21 +00:00
|
|
|
#define xpread(f, b, n, o, l) if (pread(f, b, n, o) == -1) err(ERR(), "%s", l)
|
2023-06-02 10:52:49 +00:00
|
|
|
#define handle_endianness(r) if (big_endian) xorswap_buf(r)
|
2023-06-01 09:31:21 +00:00
|
|
|
#define xpwrite(f, b, n, o, l) if (pwrite(f, b, n, o) == -1) err(ERR(), "%s", l)
|
2023-06-01 10:14:49 +00:00
|
|
|
#define xclose(f, l) if (close(f) == -1) err(ERR(), "%s", l)
|
2023-06-01 09:31:21 +00:00
|
|
|
|
|
|
|
#define xorswap(x, y) x ^= y, y ^= x, x ^= y
|
|
|
|
#define word(pos16, partnum) buf16[pos16 + (partnum << 11)]
|
|
|
|
|
2023-06-01 07:40:01 +00:00
|
|
|
void
|
|
|
|
xpledge(const char *promises, const char *execpromises)
|
|
|
|
{
|
|
|
|
(void)promises; (void)execpromises;
|
|
|
|
#ifdef __OpenBSD__
|
|
|
|
if (pledge(promises, execpromises) == -1)
|
|
|
|
err(ERR(), "pledge");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
xunveil(const char *path, const char *permissions)
|
|
|
|
{
|
|
|
|
(void)path; (void)permissions;
|
|
|
|
#ifdef __OpenBSD__
|
|
|
|
if (unveil(path, permissions) == -1)
|
|
|
|
err(ERR(), "unveil");
|
|
|
|
#endif
|
|
|
|
}
|