203 lines
6.5 KiB
C
203 lines
6.5 KiB
C
/*
|
|
* fflash.h
|
|
*
|
|
* Copyright 2026
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
* MA 02110-1301, USA.
|
|
*
|
|
*
|
|
*/
|
|
|
|
#if (!defined(__LIB_fflash_H_))
|
|
#define __LIB_fflash_H_
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
|
|
typedef FILE* file_t;
|
|
typedef unsigned char bool_t;
|
|
|
|
#define FFLASH_VERSION "1.0"
|
|
|
|
#define true 1
|
|
#define false 0
|
|
|
|
static void __help(void) {
|
|
puts (
|
|
"FFlash CLI V" FFLASH_VERSION "\n"
|
|
" " "Запись обаза в CR файл" "\n"
|
|
"Синтаксис:" "\n"
|
|
" " "-v / --version - Вывести версию и выйти" "\n"
|
|
" " "--help / -h - Показать эту справку" "\n"
|
|
"Для нормальной работы следует вызывать без опций:" "\n"
|
|
" " "fflash" "\0"
|
|
);
|
|
}
|
|
|
|
static void fflash_error(char* _err) {
|
|
printf("\033[1mfflash: \033[91m%s\033[0m\n", _err);
|
|
}
|
|
|
|
static void __drop_newline(char* str) {
|
|
size_t len = strlen(str) - 1;
|
|
if (str[len] == '\n') {
|
|
str[len] = '\0';
|
|
}
|
|
}
|
|
|
|
static int __create_iso(void) {
|
|
char* ptr = NULL;
|
|
{
|
|
unsigned long block_size = 256;
|
|
unsigned long block_count = 65536;
|
|
char isofilename[256];
|
|
char outfilename[256];
|
|
strcpy(isofilename, "/dev/zero");
|
|
strcpy(outfilename, "/dev/null");
|
|
fputs("Файл образа: ", stdout);
|
|
if (fgets(isofilename, 250, stdin) == NULL) fflash_error("Ошибка чтения ввода!");
|
|
fputs("Файл записи: ", stdout);
|
|
if (fgets(outfilename, 250, stdin) == NULL) fflash_error("Ошибка чтения ввода!");
|
|
fputs("Размер блока: ", stdout);
|
|
if (scanf("%lu", &block_size) == EOF) fflash_error("Ошибка чтения ввода!");
|
|
fputs("Количество блоков (опционально): ", stdout);
|
|
int stat;
|
|
__drop_newline(isofilename);
|
|
__drop_newline(outfilename);
|
|
if ((stat = scanf("%lu", &block_count)) == 0 || stat == EOF) puts(" (Использовано значение 65536)");
|
|
ptr = (char*)malloc(sizeof(char) * (strlen(isofilename) + strlen(outfilename) + 64));
|
|
if (ptr == NULL) {
|
|
fflash_error("Ошибка выделения памяти!");
|
|
return EXIT_FAILURE;
|
|
}
|
|
if(sprintf(ptr, "dd if=%s of=%s bs=%lu count=%lu status=progress", isofilename, outfilename, block_size, block_count) == 0) {
|
|
fflash_error("Ошибка сборки!");
|
|
free(ptr);
|
|
return EXIT_FAILURE;
|
|
}
|
|
}
|
|
puts(ptr);
|
|
if (system(ptr) != EXIT_SUCCESS) {
|
|
fflash_error("Ошибка записи!");
|
|
free(ptr);
|
|
return EXIT_FAILURE;
|
|
};
|
|
free(ptr);
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
static int __clear_iso(void) {
|
|
char isofilename[256];
|
|
strcpy(isofilename, "/dev/zero");
|
|
fputs("Файл образа: ", stdout);
|
|
if (fgets(isofilename, 255, stdin) == NULL) fflash_error("Ошибка чтения ввода!");
|
|
__drop_newline(isofilename);
|
|
char* ptr = malloc((strlen(isofilename) + 32) * sizeof(char));
|
|
if (ptr == NULL) {
|
|
fflash_error("Ошибка выделения памяти!");
|
|
return EXIT_FAILURE;
|
|
}
|
|
if(sprintf(ptr, "dd if=/dev/null of=%s", isofilename) == 0) {
|
|
fflash_error("Ошибка сборки!");
|
|
free(ptr);
|
|
return EXIT_FAILURE;
|
|
}
|
|
if (system(ptr) != EXIT_SUCCESS) {
|
|
fflash_error("Ошибка записи!");
|
|
free(ptr);
|
|
return EXIT_FAILURE;
|
|
};
|
|
free(ptr);
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
static int __edit_iso(void) {
|
|
puts (
|
|
"Выберите режим:" "\n"
|
|
" " "1. Очистить образ" "\n"
|
|
" " "2. Обновить образ" "\n"
|
|
" " "3. Выход" "\0"
|
|
);
|
|
fputs("Номер: ", stdout);
|
|
unsigned char num;
|
|
if (scanf("%d", (int*)&num) == EOF || num > 3) {
|
|
fflash_error("Неизвестный номер!");
|
|
return EXIT_FAILURE;
|
|
}
|
|
getchar();
|
|
switch (num) {
|
|
case 1: return __clear_iso();
|
|
case 2: return __create_iso();
|
|
}
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
|
|
static int __normal_start(void) {
|
|
puts (
|
|
"Выберите режим:" "\n"
|
|
" " "1. Создание и запись образа" "\n"
|
|
" " "2. Загрузка образа из файла для редактирования" "\n"
|
|
" " "3. Вывод справки" "\n"
|
|
" " "4. Выход" "\0"
|
|
);
|
|
fputs("Номер: ", stdout);
|
|
int num;
|
|
if (scanf("%d", ((int*)(&num))) == EOF || num > 4 || num < 1) {
|
|
fflash_error("Неизвестный номер!");
|
|
return EXIT_FAILURE;
|
|
}
|
|
getchar();
|
|
switch (num) {
|
|
case 1: return __create_iso();
|
|
case 2: return __edit_iso();
|
|
case 3: __help(); break;
|
|
default: break;
|
|
}
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
int _lib_fflash_main(int argc, char** argv) {
|
|
unsigned int t = 1;
|
|
while(t < (unsigned int)argc) {
|
|
char* str = argv[t];
|
|
if (str[0] == '-') {
|
|
bool_t is_add = false;
|
|
if (str[1] == '-') is_add = true;
|
|
switch (str[1 + is_add]) {
|
|
case 'h':
|
|
__help();
|
|
return EXIT_SUCCESS;
|
|
case 'v':
|
|
puts(FFLASH_VERSION);
|
|
return EXIT_SUCCESS;
|
|
default:
|
|
fflash_error("Неизвестная опция!");
|
|
return EXIT_FAILURE;
|
|
break;
|
|
}
|
|
} else {
|
|
fflash_error("Неизвестная опция!");
|
|
return EXIT_FAILURE;
|
|
}
|
|
}
|
|
return __normal_start();
|
|
}
|
|
|
|
#endif /* __LIB_fflash_H_ */
|
|
|