logo

live-bootstrap

Mirror of <https://github.com/fosslinux/live-bootstrap>
commit: 192221af22b0112a36b361a048a725809f458145
parent 71505bc8b94796f473b79fc3ef2f5c9250c0bed4
Author: fosslinux <fosslinux@aussies.space>
Date:   Fri, 12 Feb 2021 18:20:18 +1100

Add fletcher16-gen developer util

1. I'm not convinced our fletcher16 implementation is proper
2. It is not in coreutils

So we add some basic code to do that.

This is also the first dev-util, so add some documentation to DEVEL.md.

Diffstat:

MDEVEL.md10++++++++++
Adev-utils/fletcher16-gen/.gitignore8++++++++
Adev-utils/fletcher16-gen/compile-fletcher16-gen.sh42++++++++++++++++++++++++++++++++++++++++++
Adev-utils/fletcher16-gen/fletcher16-gen.c40++++++++++++++++++++++++++++++++++++++++
Adev-utils/m2-functions/file_print.c17+++++++++++++++++
Adev-utils/m2-functions/in_set.c21+++++++++++++++++++++
Adev-utils/m2-functions/match.c24++++++++++++++++++++++++
Adev-utils/m2-functions/numerate_number.c159+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Adev-utils/m2-functions/require.c20++++++++++++++++++++
Adev-utils/m2-functions/string.c59+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
10 files changed, 400 insertions(+), 0 deletions(-)

diff --git a/DEVEL.md b/DEVEL.md @@ -49,6 +49,16 @@ Permissable folders: - `src`: the upstream unmodified source code. This may be either a submodule or nonexistant. +Furthermore, there is a special top-level dir `dev-utils`. In here are +appropriate utilities that are often useful for development and not generally +included on normal systems, or are specific to live-bootstrap. Each program +should be contained within its own directory and include: + +- source code +- compilation script + +The directory m2-functions is used for M2-Planet functions (currently). + ## Conventions - **Patches:** diff --git a/dev-utils/fletcher16-gen/.gitignore b/dev-utils/fletcher16-gen/.gitignore @@ -0,0 +1,8 @@ +# SPDX-FileCopyrightText: 2021 fosslinux <fosslinux@aussies.space> +# +# SPDX-License-Identifier: MIT + +fletcher16.M1 +fletcher16-footer.M1 +fletcher16.hex2 +fletcher16-gen diff --git a/dev-utils/fletcher16-gen/compile-fletcher16-gen.sh b/dev-utils/fletcher16-gen/compile-fletcher16-gen.sh @@ -0,0 +1,42 @@ +#!/bin/sh + +# SPDX-FileCopyrightText: 2021 fosslinux <fosslinux@aussies.space> +# +# SPDX-License-Identifier: GPL-3.0-or-later + +set -ex + +M2-Planet \ + --architecture x86 \ + -f ../m2-functions/in_set.c \ + -f ../m2-functions/file_print.c \ + -f ../m2-functions/numerate_number.c \ + -f ../m2-functions/string.c \ + -f ../../sysa/mescc-tools-seed/src/mescc-tools-seed/M2-Planet/test/common_x86/functions/file.c \ + -f ../../sysa/mescc-tools-seed/src/mescc-tools-seed/M2-Planet/test/common_x86/functions/exit.c \ + -f ../m2-functions/require.c \ + -f ../../sysa/mescc-tools-seed/src/mescc-tools-seed/M2-Planet/test/common_x86/functions/malloc.c \ + -f ../../sysa/mescc-tools-seed/src/mescc-tools-seed/M2-Planet/functions/calloc.c \ + -f fletcher16-gen.c \ + -o fletcher16.M1 \ + --debug + +blood-elf -f fletcher16.M1 -o fletcher16-footer.M1 + +M1 \ + -f ../../sysa/mescc-tools-seed/src/mescc-tools-seed/M2-Planet/test/common_x86/x86_defs.M1 \ + -f ../../sysa/mescc-tools-seed/src/mescc-tools-seed/M2-Planet/test/common_x86/libc-core.M1 \ + -f fletcher16.M1 \ + -f fletcher16-footer.M1 \ + --LittleEndian \ + --architecture x86 \ + -o fletcher16.hex2 + +hex2 \ + -f ../../sysa/mescc-tools-seed/src/mescc-tools-seed/M2-Planet/test/common_x86/ELF-i386-debug.hex2 \ + -f fletcher16.hex2 \ + --LittleEndian \ + --architecture x86 \ + --BaseAddress 0x8048000 \ + -o fletcher16-gen \ + --exec_enable diff --git a/dev-utils/fletcher16-gen/fletcher16-gen.c b/dev-utils/fletcher16-gen/fletcher16-gen.c @@ -0,0 +1,40 @@ +/* + * SPDX-FileCopyrightText: 2021 fosslinux <fosslinux@aussies.space> + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include <stdlib.h> +#include <stdio.h> + +#define BUF_SIZE 1024 +// CONSTANT BUF_SIZE 1024 +#define PATH_MAX 512 +// CONSTANT PATH_MAX 512 + +int fletch16(FILE* fp) +{ + long sum1 = 0; + long sum2 = 0; + int index; + int c = fgetc(fp); + while(c != EOF) + { + sum1 = (sum1 + c) % 255; + sum2 = (sum2 + sum1) % 255; + c = fgetc(fp); + } + + return (sum2 << 8) | sum1; +} + +int main(int argc, char** argv) +{ + /* Open the file containing the checksums */ + FILE* fp = fopen(argv[1], "r"); + require(fp != NULL, prepend_string( + prepend_string("Error opening checksum file ", argv[1]), "\n")); + int csum = fletch16(fp); + file_print(prepend_string(prepend_string(prepend_string( + numerate_number(csum), " "), argv[1]), "\n"), stdout); +} diff --git a/dev-utils/m2-functions/file_print.c b/dev-utils/m2-functions/file_print.c @@ -0,0 +1,17 @@ +/* + * SPDX-FileCopyrightText: 2016 Jeremiah Orians + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include<stdio.h> +// void fputc(char s, FILE* f); + +void file_print(char* s, FILE* f) +{ + while(0 != s[0]) + { + fputc(s[0], f); + s = s + 1; + } +} diff --git a/dev-utils/m2-functions/in_set.c b/dev-utils/m2-functions/in_set.c @@ -0,0 +1,21 @@ +/* + * SPDX-FileCopyrightText: 2016 Jeremiah Orians + * SPDX-FileCopyrightText: 2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org> + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#define FALSE 0 +// CONSTANT FALSE 0 +#define TRUE 1 +// CONSTANT TRUE 1 + +int in_set(int c, char* s) +{ + while(0 != s[0]) + { + if(c == s[0]) return TRUE; + s = s + 1; + } + return FALSE; +} diff --git a/dev-utils/m2-functions/match.c b/dev-utils/m2-functions/match.c @@ -0,0 +1,24 @@ +/* + * SPDX-FileCopyrightText: 2016 Jeremiah Orians + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#define FALSE 0 +// CONSTANT FALSE 0 +#define TRUE 1 +// CONSTANT TRUE 1 + +int match(char* a, char* b) +{ + int i = -1; + do + { + i = i + 1; + if(a[i] != b[i]) + { + return FALSE; + } + } while((0 != a[i]) && (0 !=b[i])); + return TRUE; +} diff --git a/dev-utils/m2-functions/numerate_number.c b/dev-utils/m2-functions/numerate_number.c @@ -0,0 +1,159 @@ +/* + * SPDX-FileCopyrightText: 2016 Jeremiah Orians + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include<stdlib.h> +// void* calloc(int count, int size); +#define TRUE 1 +//CONSTANT TRUE 1 +#define FALSE 0 +//CONSTANT FALSE 0 +int in_set(int c, char* s); +//CONSTANT NULL 0 + +char* numerate_number(int a) +{ + char* result = calloc(16, sizeof(char)); + if(NULL == result) return NULL; + + int i = 0; + + /* Deal with Zero case */ + if(0 == a) + { + result[0] = '0'; + return result; + } + + /* Deal with negatives */ + if(0 > a) + { + result[0] = '-'; + i = 1; + a = a * -1; + } + + /* Using the largest 10^n number possible in 32bits */ + int divisor = 0x3B9ACA00; + /* Skip leading Zeros */ + while(0 == (a / divisor)) divisor = divisor / 10; + + /* Now simply collect numbers until divisor is gone */ + while(0 < divisor) + { + result[i] = ((a / divisor) + 48); + a = a % divisor; + divisor = divisor / 10; + i = i + 1; + } + + return result; +} + +int char2hex(int c) +{ + if (c >= '0' && c <= '9') return (c - 48); + else if (c >= 'a' && c <= 'f') return (c - 87); + else if (c >= 'A' && c <= 'F') return (c - 55); + else return -1; +} + +int hex2char(int c) +{ + if((c >= 0) && (c <= 9)) return (c + 48); + else if((c >= 10) && (c <= 15)) return (c + 55); + else return -1; +} + +int char2dec(int c) +{ + if (c >= '0' && c <= '9') return (c - 48); + else return -1; +} + +int dec2char(int c) +{ + if((c >= 0) && (c <= 9)) return (c + 48); + else return -1; +} + +int index_number(char* s, char c) +{ + int i = 0; + while(s[i] != c) + { + i = i + 1; + if(0 == s[i]) return -1; + } + return i; +} + +int toupper(int c) +{ + if(in_set(c, "abcdefghijklmnopqrstuvwxyz")) return (c & 0xDF); + return c; +} + +int set_reader(char* set, int mult, char* input) +{ + int n = 0; + int i = 0; + int hold; + int negative_p = FALSE; + + if(input[0] == '-') + { + negative_p = TRUE; + i = i + 1; + } + + while(in_set(input[i], set)) + { + n = n * mult; + hold = index_number(set, toupper(input[i])); + + /* Input managed to change between in_set and index_number */ + if(-1 == hold) return 0; + n = n + hold; + i = i + 1; + } + + /* loop exited before NULL and thus invalid input */ + if(0 != input[i]) return 0; + + if(negative_p) + { + n = 0 - n; + } + + return n; +} + +int numerate_string(char *a) +{ + /* If NULL string */ + if(0 == a[0]) return 0; + + /* Deal with binary*/ + else if ('0' == a[0] && 'b' == a[1]) + { + return set_reader("01", 2, a+2); + } + /* Deal with hex */ + else if ('0' == a[0] && 'x' == a[1]) + { + return set_reader("0123456789ABCDEFabcdef", 16, a+2); + } + /* Deal with octal */ + else if('0' == a[0]) + { + return set_reader("01234567", 8, a+1); + } + /* Deal with decimal */ + else + { + return set_reader("0123456789", 10, a); + } +} diff --git a/dev-utils/m2-functions/require.c b/dev-utils/m2-functions/require.c @@ -0,0 +1,20 @@ +/* + * SPDX-FileCopyrightText: 2016 Jeremiah Orians + * SPDX-FileCopyrightText: 2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org> + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include<stdio.h> +#include<stdlib.h> + +void file_print(char* s, FILE* f); + +void require(int bool, char* error) +{ + if(!bool) + { + file_print(error, stderr); + exit(EXIT_FAILURE); + } +} diff --git a/dev-utils/m2-functions/string.c b/dev-utils/m2-functions/string.c @@ -0,0 +1,59 @@ +/* + * SPDX-FileCopyrightText: 2016 Jeremiah Orians + * + * SPDX-License-Identifier: GPL-3.0-or-later + */ + +#include<stdlib.h> +#include<stdio.h> +#define MAX_STRING 4096 +//CONSTANT MAX_STRING 4096 +// void* calloc(int count, int size); +void file_print(char* s, FILE* f); + +char* copy_string(char* target, char* source) +{ + while(0 != source[0]) + { + target[0] = source[0]; + target = target + 1; + source = source + 1; + } + return target; +} + +char* postpend_char(char* s, char a) +{ + char* ret = calloc(MAX_STRING, sizeof(char)); + if(NULL == ret) return NULL; + + char* hold = copy_string(ret, s); + hold[0] = a; + return ret; +} + +char* prepend_char(char a, char* s) +{ + char* ret = calloc(MAX_STRING, sizeof(char)); + if(NULL == ret) return NULL; + + ret[0] = a; + copy_string((ret+1), s); + return ret; +} + +char* prepend_string(char* add, char* base) +{ + char* ret = calloc(MAX_STRING, sizeof(char)); + if(NULL == ret) return NULL; + + copy_string(copy_string(ret, add), base); + return ret; +} + +int string_length(char* a) +{ + int i = 0; + while(0 != a[i]) i = i + 1; + return i; +}