+grub_err_t ventoy_cmd_unix_ko_fillmap(grub_extcmd_context_t ctxt, int argc, char **args)
+{
+ int i;
+ grub_file_t file;
+ grub_uint32_t magic[4];
+ grub_uint32_t len;
+
+ (void)ctxt;
+
+ if (argc != 1)
+ {
+ debug("Fillmap ko invalid argc %d\n", argc);
+ return 1;
+ }
+
+ debug("Fillmap ko %s\n", args[0]);
+
+ file = ventoy_grub_file_open(VENTOY_FILE_TYPE, "(loop)%s", args[0]);
+ if (file)
+ {
+ g_mod_override_offset = grub_iso9660_get_last_read_pos(file);
+ }
+ else
+ {
+ debug("Can't find replace ko file from %s\n", args[0]);
+ return 1;
+ }
+
+ for (i = 0; i < (int)(file->size); i += 65536)
+ {
+ magic[0] = 0;
+ grub_file_seek(file, i);
+ grub_file_read(file, magic, sizeof(magic));
+
+ if (magic[0] == VENTOY_UNIX_SEG_MAGIC0 && magic[1] == VENTOY_UNIX_SEG_MAGIC1 &&
+ magic[2] == VENTOY_UNIX_SEG_MAGIC2 && magic[3] == VENTOY_UNIX_SEG_MAGIC3)
+ {
+ debug("unix find search magic at 0x%x loop:%d\n", i, (i >> 16));
+ g_mod_override_offset += i;
+ break;
+ }
+ }
+
+ len = (grub_uint32_t)OFFSET_OF(struct g_ventoy_map, seglist) +
+ (sizeof(struct g_ventoy_seg) * g_img_chunk_list.cur_chunk);
+
+ g_ko_fillmap_len = (int)len;
+ g_ko_fillmap_data = grub_malloc(len);
+ if (!g_ko_fillmap_data)
+ {
+ g_ko_fillmap_len = 0;
+ debug("Failed to malloc fillmap data\n");
+ }
+
+ debug("Fillmap ko segnum:%u, override len:%d", g_img_chunk_list.cur_chunk, g_ko_fillmap_len);
+
+ grub_file_close(file);
+ VENTOY_CMD_RETURN(GRUB_ERR_NONE);
+}
+