From a9c539572bbc3add985e858b0843425988f90fd3 Mon Sep 17 00:00:00 2001 From: longpanda Date: Mon, 8 Nov 2021 14:55:08 +0800 Subject: [PATCH] Show a warning message if ventoy.json is in UCS-2 encoding. (#1125) ventoy.json must in UTF-8 encoding. The BOM is auto skipped if exist. --- .../grub-core/ventoy/ventoy_plugin.c | 22 +++++++++++++++++-- INSTALL/grub/grub.cfg | 13 ++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_plugin.c b/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_plugin.c index 77c833ce..ca0e96dc 100644 --- a/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_plugin.c +++ b/GRUB2/MOD_SRC/grub-2.04/grub-core/ventoy/ventoy_plugin.c @@ -2399,7 +2399,9 @@ static int ventoy_parse_plugin_config(VTOY_JSON *json, const char *isodisk) grub_err_t ventoy_cmd_load_plugin(grub_extcmd_context_t ctxt, int argc, char **args) { int ret = 0; + int offset = 0; char *buf = NULL; + grub_uint8_t *code = NULL; grub_file_t file; VTOY_JSON *json = NULL; @@ -2436,9 +2438,25 @@ grub_err_t ventoy_cmd_load_plugin(grub_extcmd_context_t ctxt, int argc, char **a return 1; } - + code = (grub_uint8_t *)buf; + if (code[0] == 0xef && code[1] == 0xbb && code[2] == 0xbf) + { + offset = 3; /* Skip UTF-8 BOM */ + } + else if ((code[0] == 0xff && code[1] == 0xfe) || (code[0] == 0xfe && code[1] == 0xff)) + { + grub_env_set("VTOY_PLUGIN_SYNTAX_ERROR", "1"); + grub_env_export("VTOY_PLUGIN_SYNTAX_ERROR"); - ret = vtoy_json_parse(json, buf); + grub_env_set("VTOY_PLUGIN_ENCODE_ERROR", "1"); + grub_env_export("VTOY_PLUGIN_ENCODE_ERROR"); + + debug("Failed to parse json string %d\n", ret); + grub_free(buf); + return 1; + } + + ret = vtoy_json_parse(json, buf + offset); if (ret) { grub_env_set("VTOY_PLUGIN_SYNTAX_ERROR", "1"); diff --git a/INSTALL/grub/grub.cfg b/INSTALL/grub/grub.cfg index 4acad0d4..531faacd 100644 --- a/INSTALL/grub/grub.cfg +++ b/INSTALL/grub/grub.cfg @@ -2178,12 +2178,19 @@ fi if [ -n "$VTOY_PLUGIN_SYNTAX_ERROR" ]; then clear - echo -e "\n Syntax error detected in ventoy.json, please check! \n" - echo -e " ventoy.json 文件中有语法错误,所有配置都不会生效,请检查!\n" + if [ -n "$VTOY_PLUGIN_ENCODE_ERROR" ]; then + echo -e "\n Encoding type for ventoy.json is not supported, please convert to UTF-8.\n" + echo -e " ventoy.json 文件编码格式不支持,请转换为 UTF-8 编码格式!\n" + else + echo -e "\n Syntax error detected in ventoy.json, please check! \n" + echo -e " ventoy.json 文件中有语法错误,所有配置都不会生效,请检查!\n" + fi + echo -e "\n press ENTER to continue (请按 回车 键继续) ..." - read vtInputKey + read vtInputKey fi + for vtTFile in ventoy.json ventoy_grub.cfg; do if [ -f $vtoy_efi_part/ventoy/$vtTFile ]; then clear