/* radare2 - LGPL - Copyright 2016-2025 - pancake */

#include <r_bin.h>

#define MENUET_VERSION(x) x[7]

#if 0
db      'MENUET00'           ; 8 byte id
dd      38                   ; required os
dd      START                ; program start
dd      I_END                ; image size
dd      0x100000             ; reguired amount of memory
dd      0x00000000           ; reserved=no extended header

org     0x0
db      'MENUET01'              ; 8 byte id
dd      1                       ; header version
dd      START                   ; program start
dd      I_END                   ; program image size
dd      0x1000                  ; required amount of memory
dd      0x1000                  ; esp
dd      0, 0                    ; no parameters, no path

0 db 'MENUET02'
8 dd 0x01
12 dd __start
16 dd __iend
20 dd __bssend
24 dd __stack
28 dd __cmdline
32 dd __pgmname
36 dd 0x0; tls map
40 dd __idata_start; секция .import
44 dd __idata_end
48 dd main

db 'MENUET02'
dd 1
dd start
dd i_end
dd mem
dd mem
dd cmdline
dd path
dd 0

#endif

static bool check(RBinFile *bf, RBuffer *b) {
	ut8 buf[8];
	if (r_buf_read_at (b, 0, buf, sizeof (buf)) != sizeof (buf)) {
		return false;
	}
	if (r_buf_size (b) >= 32 && !memcmp (buf, "MENUET0", 7)) {
		switch (buf[7]) {
		case '0':
		case '1':
		case '2':
			return true;
		}
		R_LOG_WARN ("Unsupported MENUET version header");
	}
	return false;
}

static bool load(RBinFile *bf, RBuffer *b, ut64 loadaddr) {
	return check (bf, b);
}

static ut64 baddr(RBinFile *bf) {
	return 0; // 0x800000;
}

static ut64 menuetEntry(const ut8 *buf, int buf_size) {
	switch (MENUET_VERSION (buf)) {
	case '0': return r_read_ble32 (buf + 12, false);
	case '1': return r_read_ble32 (buf + 12, false);
	case '2': return r_read_ble32 (buf + 44, false);
	}
	return UT64_MAX;
}

static RList *entries(RBinFile *bf) {
	ut8 buf[64] = {0};
	const int buf_size = R_MIN (sizeof (buf), r_buf_size (bf->buf));
	r_buf_read_at (bf->buf, 0, buf, buf_size);
	ut64 entry = menuetEntry (buf, buf_size);
	if (entry == UT64_MAX) {
		return NULL;
	}
	RList *ret = r_list_newf (free);
	if (!ret) {
		return NULL;
	}
	RBinAddr *ptr = R_NEW0 (RBinAddr);
	ptr->paddr = r_read_ble32 (buf + 12, false);
	ptr->vaddr = ptr->paddr + baddr (bf);
	r_list_append (ret, ptr);
	return ret;
}

static bool sections_vec(RBinFile *bf) {
	ut8 buf[64] = {0};
	const int buf_size = R_MIN (sizeof (buf), r_buf_size (bf->buf));
	r_buf_read_at (bf->buf, 0, buf, buf_size);
	if (!bf->bo->info) {
		return false;
	}
	RVecRBinSection_clear (&bf->bo->sections_vec);

	RBinSection *ptr = RVecRBinSection_emplace_back (&bf->bo->sections_vec);
	ptr->name = strdup ("text");
	ptr->size = r_read_ble32 (buf + 16, false);
	ptr->vsize = ptr->size + (ptr->size % 4096);
	ptr->paddr = r_read_ble32 (buf + 12, false);
	ptr->vaddr = ptr->paddr + baddr (bf);
	ptr->perm = R_PERM_RX;
	ptr->add = true;

	if (MENUET_VERSION (buf)) {
		ptr = RVecRBinSection_emplace_back (&bf->bo->sections_vec);
		ptr->name = strdup ("idata");
		const ut32 idata_start = r_read_ble32 (buf + 40, false);
		const ut32 idata_end = r_read_ble32 (buf + 44, false);
		ptr->size = idata_end - idata_start;
		ptr->vsize = ptr->size + (ptr->size % 4096);
		ptr->paddr = idata_start;
		ptr->vaddr = ptr->paddr + baddr (bf);
		ptr->perm = R_PERM_R;
		ptr->add = true;
	}

	return true;
}

static RBinInfo *info(RBinFile *bf) {
	RBinInfo *ret = R_NEW0 (RBinInfo);
	ret->file = strdup (bf->file);
	ret->bclass = strdup ("program");
	ret->rclass = strdup ("menuet");
	ret->os = strdup ("MenuetOS");
	ret->arch = strdup ("x86");
	ret->machine = strdup (ret->arch);
	ret->subsystem = strdup ("kolibri");
	ret->type = strdup ("EXEC");
	ret->bits = 32;
	ret->has_va = true;
	return ret;
}

static ut64 size(RBinFile *bf) {
	ut8 buf[4] = { 0 };
	if (!bf->bo->info) {
		bf->bo->info = info (bf);
	}
	if (!bf->bo->info) {
		return 0;
	}
	r_buf_read_at (bf->buf, 16, buf, 4);
	return (ut64)r_read_ble32 (buf, false);
}

#if !R_BIN_P9

/* inspired in http://www.phreedom.org/solar/code/tinype/tiny.97/tiny.asm */
static RBuffer *create(RBin *bin, const ut8 *code, int codelen, const ut8 *data, int datalen, RBinArchOptions *opt) {
	RBuffer *buf = r_buf_new ();
#define B(x, y) r_buf_append_bytes(buf,(const ut8 *)(x), y)
#define D(x) r_buf_append_ut32(buf, x)
	B ("MENUET01", 8);
	D (1); // header version
	D (32); // program start
	D (0x1000); // program image size
	D (0x1000); // ESP
	D (0); // no parameters
	D (0); // no path
	B (code, codelen);
	return buf;
}

RBinPlugin r_bin_plugin_menuet = {
	.meta = {
		.name = "menuet",
		.desc = "Menuet/KolibriOS executables",
		.author = "pancake",
		.license = "LGPL-3.0-only",
	},
	.load = &load,
	.size = &size,
	.check = &check,
	.baddr = &baddr,
	.entries = &entries,
	.sections_vec = &sections_vec,
	.info = &info,
	.create = &create,
};

#ifndef R2_PLUGIN_INCORE
R_API RLibStruct radare_plugin = {
	.type = R_LIB_TYPE_BIN,
	.data = &r_bin_plugin_menuet,
	.version = R2_VERSION
};
#endif
#endif
