From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.88; helo=mga01.intel.com; envelope-from=jaben.carsey@intel.com; receiver=edk2-devel@lists.01.org Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 01A782034C088 for ; Thu, 19 Oct 2017 07:43:42 -0700 (PDT) Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 19 Oct 2017 07:47:21 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.43,402,1503385200"; d="scan'208";a="1207638479" Received: from fmsmsx106.amr.corp.intel.com ([10.18.124.204]) by fmsmga001.fm.intel.com with ESMTP; 19 Oct 2017 07:47:20 -0700 Received: from fmsmsx120.amr.corp.intel.com (10.18.124.208) by FMSMSX106.amr.corp.intel.com (10.18.124.204) with Microsoft SMTP Server (TLS) id 14.3.319.2; Thu, 19 Oct 2017 07:47:20 -0700 Received: from fmsmsx103.amr.corp.intel.com ([169.254.2.182]) by fmsmsx120.amr.corp.intel.com ([169.254.15.198]) with mapi id 14.03.0319.002; Thu, 19 Oct 2017 07:47:20 -0700 From: "Carsey, Jaben" To: "Ni, Ruiyu" , "edk2-devel@lists.01.org" CC: "Li, Huajing" Thread-Topic: [PATCH] ShellPkg/alias: Fix flag parsing logic Thread-Index: AQHTSK4S66dZz0NbZUS4xukD1nPyYaLrQPNg Date: Thu, 19 Oct 2017 14:47:20 +0000 Message-ID: References: <20171019074416.381700-1-ruiyu.ni@intel.com> In-Reply-To: <20171019074416.381700-1-ruiyu.ni@intel.com> Accept-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-titus-metadata-40: eyJDYXRlZ29yeUxhYmVscyI6IiIsIk1ldGFkYXRhIjp7Im5zIjoiaHR0cDpcL1wvd3d3LnRpdHVzLmNvbVwvbnNcL0ludGVsMyIsImlkIjoiNjU0YTIzMWYtYmYyMC00ODAzLWExMGItYjU0YzcwODllODcwIiwicHJvcHMiOlt7Im4iOiJDVFBDbGFzc2lmaWNhdGlvbiIsInZhbHMiOlt7InZhbHVlIjoiQ1RQX0lDIn1dfV19LCJTdWJqZWN0TGFiZWxzIjpbXSwiVE1DVmVyc2lvbiI6IjE2LjUuOS4zIiwiVHJ1c3RlZExhYmVsSGFzaCI6IlI0Vlo0OE8ySlM5QVRldkZHZnErXC9ycXg2eWtcL1BLaUJNSlBXQ21Fa0wxST0ifQ== x-ctpclassification: CTP_IC x-originating-ip: [10.1.200.106] MIME-Version: 1.0 Subject: Re: [PATCH] ShellPkg/alias: Fix flag parsing logic X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.22 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 19 Oct 2017 14:43:43 -0000 Content-Language: en-US Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable Reviewed-by: Jaben Carsey > -----Original Message----- > From: Ni, Ruiyu > Sent: Thursday, October 19, 2017 12:44 AM > To: edk2-devel@lists.01.org > Cc: Li, Huajing ; Carsey, Jaben > > Subject: [PATCH] ShellPkg/alias: Fix flag parsing logic > Importance: High >=20 > From: Huajing Li >=20 > Existing logic to parse the flags isn't complete and cannot detect > some invalid combinations of flags. >=20 > Contributed-under: TianoCore Contribution Agreement 1.1 > Signed-off-by: Ruiyu Ni > Cc: Jaben Carsey > --- > .../Library/UefiShellLevel3CommandsLib/Alias.c | 210 ++++++++++++++-= -- > ---- > 1 file changed, 145 insertions(+), 65 deletions(-) >=20 > diff --git a/ShellPkg/Library/UefiShellLevel3CommandsLib/Alias.c > b/ShellPkg/Library/UefiShellLevel3CommandsLib/Alias.c > index daf46a9f65..3e00eb1d55 100644 > --- a/ShellPkg/Library/UefiShellLevel3CommandsLib/Alias.c > +++ b/ShellPkg/Library/UefiShellLevel3CommandsLib/Alias.c > @@ -18,6 +18,37 @@ > #include >=20 > /** > + Print out single alias registered with the Shell. > + > + @param[in] Alias Points to the NULL-terminated shell alia= s. > + If this parameter is NULL, then all > + aliases will be returned in ReturnedData= . > + @retval SHELL_SUCCESS the printout was sucessful > +**/ > +SHELL_STATUS > +PrintSingleShellAlias( > + IN CONST CHAR16 *Alias > + ) > +{ > + CONST CHAR16 *ConstAliasVal; > + SHELL_STATUS ShellStatus; > + BOOLEAN Volatile; > + > + ShellStatus =3D SHELL_SUCCESS; > + ConstAliasVal =3D gEfiShellProtocol->GetAlias (Alias, &Volatile); > + if (ConstAliasVal =3D=3D NULL) { > + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), > gShellLevel3HiiHandle, L"alias", Alias); > + ShellStatus =3D SHELL_INVALID_PARAMETER; > + } else { > + if (ShellCommandIsOnAliasList (Alias)) { > + Volatile =3D FALSE; > + } > + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_ALIAS_OUTPUT), > gShellLevel3HiiHandle, !Volatile ? L' ' : L'*', Alias, ConstAliasVal); > + } > + return ShellStatus; > +} > + > +/** > Print out each alias registered with the Shell. >=20 > @retval STATUS_SUCCESS the printout was sucessful > @@ -30,11 +61,7 @@ PrintAllShellAlias( > { > CONST CHAR16 *ConstAllAliasList; > CHAR16 *Alias; > - CONST CHAR16 *Command; > CHAR16 *Walker; > - BOOLEAN Volatile; > - > - Volatile =3D FALSE; >=20 > ConstAllAliasList =3D gEfiShellProtocol->GetAlias(NULL, NULL); > if (ConstAllAliasList =3D=3D NULL) { > @@ -53,11 +80,7 @@ PrintAllShellAlias( > Walker[0] =3D CHAR_NULL; > Walker =3D Walker + 1; > } > - Command =3D gEfiShellProtocol->GetAlias(Alias, &Volatile); > - if (ShellCommandIsOnAliasList(Alias)) { > - Volatile =3D FALSE; > - } > - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_ALIAS_OUTPUT), > gShellLevel3HiiHandle, !Volatile?L' ':L'*', Alias, Command); > + PrintSingleShellAlias(Alias); > } while (Walker !=3D NULL && Walker[0] !=3D CHAR_NULL); >=20 > FreePool(Alias); > @@ -65,9 +88,58 @@ PrintAllShellAlias( > return (SHELL_SUCCESS); > } >=20 > +/** > + Changes a shell command alias. > + > + This function creates an alias for a shell command or if Alias is NULL= it will > delete an existing alias. > + > + > + @param[in] Command Points to the NULL-terminated shell > command or existing alias. > + @param[in] Alias Points to the NULL-terminated alias for = the shell > command. If this is NULL, and > + Command refers to an alias, that alias w= ill be deleted. > + @param[in] Replace If TRUE and the alias already exists, th= en the > existing alias will be replaced. If > + FALSE and the alias already exists, then= the existing alias is > unchanged and > + EFI_ACCESS_DENIED is returned. > + @param[in] Volatile if TRUE the Alias being set will be stor= ed in a > volatile fashion. if FALSE the > + Alias being set will be stored in a non-= volatile fashion. > + > + @retval SHELL_SUCCESS Alias created or deleted successfully. > + @retval SHELL_NOT_FOUND the Alias intended to be deleted was not > found > + @retval SHELL_ACCESS_DENIED The alias is a built-in alias or already > existed and Replace was set to > + FALSE. > + @retval SHELL_DEVICE_ERROR Command is null or the empty string. > +**/ > +SHELL_STATUS > +ShellLevel3CommandsLibSetAlias( > + IN CONST CHAR16 *Command, > + IN CONST CHAR16 *Alias, > + IN BOOLEAN Replace, > + IN BOOLEAN Volatile > + ) > +{ > + SHELL_STATUS ShellStatus; > + EFI_STATUS Status; > + > + ShellStatus =3D SHELL_SUCCESS; > + Status =3D gEfiShellProtocol->SetAlias (Command, Alias, Replace, Volat= ile); > + if (EFI_ERROR(Status)) { > + if (Status =3D=3D EFI_ACCESS_DENIED) { > + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_AD), > gShellLevel3HiiHandle, L"alias"); > + ShellStatus =3D SHELL_ACCESS_DENIED; > + } else if (Status =3D=3D EFI_NOT_FOUND) { > + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN > (STR_GEN_ERR_NOT_FOUND), gShellLevel3HiiHandle, L"alias", Command); > + ShellStatus =3D SHELL_NOT_FOUND; > + } else { > + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_UK), > gShellLevel3HiiHandle, L"alias", Status); > + ShellStatus =3D SHELL_DEVICE_ERROR; > + } > + } > + return ShellStatus; > +} > + > STATIC CONST SHELL_PARAM_ITEM ParamList[] =3D { > {L"-v", TypeFlag}, > - {L"-d", TypeFlag}, > + {L"-d", TypeValue}, > {NULL, TypeMax} > }; >=20 > @@ -90,9 +162,10 @@ ShellCommandRunAlias ( > SHELL_STATUS ShellStatus; > CONST CHAR16 *Param1; > CONST CHAR16 *Param2; > + CONST CHAR16 *ParamStrD; > CHAR16 *CleanParam2; > - CONST CHAR16 *ConstAliasVal; > - BOOLEAN Volatile; > + BOOLEAN DeleteFlag; > + BOOLEAN VolatileFlag; >=20 > ProblemParam =3D NULL; > ShellStatus =3D SHELL_SUCCESS; > @@ -123,9 +196,13 @@ ShellCommandRunAlias ( > Param1 =3D ShellCommandLineGetRawValue(Package, 1); > Param2 =3D ShellCommandLineGetRawValue(Package, 2); >=20 > + DeleteFlag =3D ShellCommandLineGetFlag (Package, L"-d"); > + VolatileFlag =3D ShellCommandLineGetFlag (Package, L"-v"); > + > if (Param2 !=3D NULL) { > CleanParam2 =3D AllocateCopyPool (StrSize(Param2), Param2); > if (CleanParam2 =3D=3D NULL) { > + ShellCommandLineFreeVarList (Package); > return SHELL_OUT_OF_RESOURCES; > } >=20 > @@ -135,65 +212,68 @@ ShellCommandRunAlias ( > } > } >=20 > - // > - // check for "-?" > - // > - if (ShellCommandLineGetFlag(Package, L"-?")) { > - ASSERT(FALSE); > - } > - if (ShellCommandLineGetCount(Package) =3D=3D 1) { > - // > - // print out alias' > - // > - Status =3D PrintAllShellAlias(); > - } else if (ShellCommandLineGetFlag(Package, L"-d")) { > - // > - // delete an alias > - // > - Status =3D gEfiShellProtocol->SetAlias(Param1, NULL, TRUE, FALSE); > - if (EFI_ERROR(Status)) { > - if (Status =3D=3D EFI_ACCESS_DENIED) { > - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_AD), > gShellLevel3HiiHandle, L"alias"); > - ShellStatus =3D SHELL_ACCESS_DENIED; > - } else if (Status =3D=3D EFI_NOT_FOUND) { > - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN > (STR_GEN_ERR_NOT_FOUND), gShellLevel3HiiHandle, L"alias", Param1); > - ShellStatus =3D SHELL_NOT_FOUND; > - } else { > - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_UK), > gShellLevel3HiiHandle, L"alias", Status); > - ShellStatus =3D SHELL_DEVICE_ERROR; > - } > + if (!DeleteFlag && !VolatileFlag) { > + switch (ShellCommandLineGetCount (Package)) { > + case 1: > + // > + // "alias" > + // > + ShellStatus =3D PrintAllShellAlias (); > + break; > + case 2: > + // > + // "alias Param1" > + // > + ShellStatus =3D PrintSingleShellAlias (Param1); > + break; > + case 3: > + // > + // "alias Param1 CleanParam2" > + // > + ShellStatus =3D ShellLevel3CommandsLibSetAlias (CleanParam2, P= aram1, > FALSE, VolatileFlag); > + break; > + default: > + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY)= , > gShellLevel3HiiHandle, L"alias"); > + ShellStatus =3D SHELL_INVALID_PARAMETER; > } > - } else if (ShellCommandLineGetCount(Package) =3D=3D 3) { > - // > - // must be adding an alias > - // > - Status =3D gEfiShellProtocol->SetAlias(CleanParam2, Param1, FALSE, > ShellCommandLineGetFlag(Package, L"-v")); > - if (EFI_ERROR(Status)) { > - if (Status =3D=3D EFI_ACCESS_DENIED) { > - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_AD), > gShellLevel3HiiHandle, L"alias"); > - ShellStatus =3D SHELL_ACCESS_DENIED; > + } else if (DeleteFlag) { > + if (VolatileFlag || ShellCommandLineGetCount (Package) > 1) { > + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), > gShellLevel3HiiHandle, L"alias"); > + ShellStatus =3D SHELL_INVALID_PARAMETER; > + } else { > + ParamStrD =3D ShellCommandLineGetValue (Package, L"-d"); > + if (ParamStrD =3D=3D NULL) { > + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), > gShellLevel3HiiHandle, L"alias"); > + ShellStatus =3D SHELL_INVALID_PARAMETER; > } else { > - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_ERR_UK), > gShellLevel3HiiHandle, L"alias", Status); > - ShellStatus =3D SHELL_DEVICE_ERROR; > + // > + // Delete an alias: "alias -d ParamStrD" > + // > + ShellStatus =3D ShellLevel3CommandsLibSetAlias (ParamStrD, NUL= L, > TRUE, FALSE); > } > } > - } else if (ShellCommandLineGetCount(Package) =3D=3D 2) { > + } else { > // > - // print out a single alias > + // Set volatile alias. > // > - ConstAliasVal =3D gEfiShellProtocol->GetAlias(Param1, &Volatile); > - if (ConstAliasVal =3D=3D NULL) { > - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), > gShellLevel3HiiHandle, L"alias", Param1); > - ShellStatus =3D SHELL_INVALID_PARAMETER; > - } else { > - if (ShellCommandIsOnAliasList(Param1)) { > - Volatile =3D FALSE; > - } > - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_ALIAS_OUTPUT), > gShellLevel3HiiHandle, !Volatile?L' ':L'*', Param1, ConstAliasVal); > - } > - } else { > - ShellPrintHiiEx(-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY), > gShellLevel3HiiHandle, L"alias"); > - ShellStatus =3D SHELL_INVALID_PARAMETER; > + ASSERT (VolatileFlag); > + ASSERT (!DeleteFlag); > + switch (ShellCommandLineGetCount (Package)) { > + case 1: > + case 2: > + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_FEW), > gShellLevel3HiiHandle, L"alias"); > + ShellStatus =3D SHELL_INVALID_PARAMETER; > + break; > + case 3: > + // > + // "alias -v Param1 CleanParam2" > + // > + ShellStatus =3D ShellLevel3CommandsLibSetAlias (CleanParam2, P= aram1, > FALSE, VolatileFlag); > + break; > + default: > + ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_TOO_MANY)= , > gShellLevel3HiiHandle, L"alias"); > + ShellStatus =3D SHELL_INVALID_PARAMETER; > + } > } > // > // free the command line package > -- > 2.12.2.windows.2