Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752792AbdFGTUL (ORCPT ); Wed, 7 Jun 2017 15:20:11 -0400 Received: from mail-bl2nam02on0052.outbound.protection.outlook.com ([104.47.38.52]:19818 "EHLO NAM02-BL2-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1752056AbdFGTTY (ORCPT ); Wed, 7 Jun 2017 15:19:24 -0400 Authentication-Results: vger.kernel.org; dkim=none (message not signed) header.d=none;vger.kernel.org; dmarc=none action=none header.from=amd.com; From: Tom Lendacky Subject: [PATCH v6 33/34] x86/boot: Add early cmdline parsing for options with arguments To: linux-arch@vger.kernel.org, linux-efi@vger.kernel.org, kvm@vger.kernel.org, linux-doc@vger.kernel.org, x86@kernel.org, kexec@lists.infradead.org, linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, linux-mm@kvack.org, iommu@lists.linux-foundation.org Cc: Rik van Riel , Radim =?utf-8?b?S3LEjW3DocWZ?= , Toshimitsu Kani , Arnd Bergmann , Jonathan Corbet , Matt Fleming , "Michael S. Tsirkin" , Joerg Roedel , Konrad Rzeszutek Wilk , Paolo Bonzini , Larry Woodman , Brijesh Singh , Ingo Molnar , Borislav Petkov , Andy Lutomirski , "H. Peter Anvin" , Andrey Ryabinin , Alexander Potapenko , Dave Young , Thomas Gleixner , Dmitry Vyukov Date: Wed, 07 Jun 2017 14:19:05 -0500 Message-ID: <20170607191905.28645.58429.stgit@tlendack-t1.amdoffice.net> In-Reply-To: <20170607191309.28645.15241.stgit@tlendack-t1.amdoffice.net> References: <20170607191309.28645.15241.stgit@tlendack-t1.amdoffice.net> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SN1PR19CA0036.namprd19.prod.outlook.com (10.163.220.46) To MWHPR12MB1152.namprd12.prod.outlook.com (10.169.204.16) X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: MWHPR12MB1152: X-MS-Office365-Filtering-Correlation-Id: 10d17e78-85ab-4d26-a6fa-08d4adda1360 X-MS-Office365-Filtering-HT: Tenant X-Microsoft-Antispam: UriScan:;BCL:0;PCL:0;RULEID:(22001)(48565401081)(201703131423075)(201703031133081);SRVR:MWHPR12MB1152; X-Microsoft-Exchange-Diagnostics: 1;MWHPR12MB1152;3:fM8k4NVJ+1qnFlCgrAbDzO1hxYaO/4Sml2D6m2xmj026JdsWCPGm1OVfGeWSXp+NvbDc0+7JoaZVlwhrMcf+jSogb6boqjXrWjGF/kdGZqnynn/stHJ9q15C8p79tShOfKvXUojr0h50wTNkQeJdNc8sZUxuraquGIrKZ14JMbQbSCSbvt2iQEcjJqQ1y618UTsG/O6KmSIU0LElxlLQf3mtpqQL71MODG9kXAfPeVlECYolXhuoJrqE/Ov1Hwr0+t8Ed8MwpTb/Kzr6y+tfxiq5JvYoyMLnKk5ZFfdv6h7/0JM6X8Kv32iKtEg3wSJe3ytWd9Xo0ff+OP3dmsNe8LQgEPacFe20fWBqYXm0Xmg=;25:ma1To3fzv1x6rY7/tzT30gIPrsoyhd2momzwlD6l1GHkh16Y+JXfjGHlfjIbPHRUURv/JrhufzXFIzam4xKukgqinHpZvPP//SL1CrusoyQhDyW1kZwx0cnx9e5DF9hRxX4VcIjejBKqf6FVYlFSPGgXovFJKClrQLFS975iBZLK9T7dra1SRW67D25SBlPSQmT/eMV4pWwl8kMcvCD97LPAsxPgXAqyOdmN7ohbUkZ3KVcWHNowdP3SqShKxzNGnw0QWm2gdwfoqJTlVSC3+54j8aXvyHIPzs/OU8bXieZGlOQp/Im3HdgKtin8nxrexSAQRioEjDPq3k1uk1i1C0AgsWrbUoYtDAq6lXQxXjB8yDSWEz8LC3/DuftRmqc7hr+hACJ5lsdcywjyEoLk2EOxX1gIUp2KqhwTkFrGOli2bCd5Q8wvjin8x3R7mCfGloRqFFuIC6ggi0Wi/mVLFFaouQu629rUpTKbzCWi7dE= X-Microsoft-Exchange-Diagnostics: 1;MWHPR12MB1152;31:dJckdupxAwTXXgQfk0dy9d0W19YYmK6Lkt/TmNvT2V1HBJUFDN63RjAcBVAr97I7o2zxLHc/NOF4we58pgdbkcZwPva6OgtAFRjhABFAruRnGuSCxnnJ/TEst+K8R2sruqCKl+Yh8pndq4RKPrXL5MjoIDIwrJaYQJOiuag++FhjcM7uHAznoReNBQsuzxv1DVq+lR8sgyk/eOdGv4CWYJXWI14f17Qym2IBojgWcg4=;20:fEH7Csc4wnwjVvRlkqKghaYzDPC1wf0JG5s4lrWTBkv2y8OigXEBgx7WZ+Tbj4vX3nPqEm8gS+y7FcxOfXwxdBBJrKhnAZvaaa8dZpimAqtK1FYMjeLjYT7WhhHajLHSorKr2mQFe0qRwMBPqflqokNRrOKdLV3bsgQgiohisXGkY1p1TSbX4MEeJ0xedA9lnSq0bDTlBWYl8fDf5Sh4R+uj+3AgSkOJrjrgphEA2WcPdw/xCoShHCap/s2BvvKx78T7ZLvmHaTVw7hr2dhyXFbgZgi2pBKs20QpwF11X0gWLe3T8Njc+KmeAFtBBBamBM1PPbWKX/z9bNOutcWtQt1qLL7voLv10IAwYu7Cc4cVcPoXuuf0uWxNcsinTehWaQPQGOJFFd6Ye+SCYzT6QanTNaTLAHCj388sjZKL9j8UkyQM1sRqPzm7O0Ij4kRsjNJOIOngIq1Bt/seKtnQfqlXLKkMtDN5egvYUkcSHZIVEIxoOonJ6UiOtdBbqlWB X-Microsoft-Antispam-PRVS: X-Exchange-Antispam-Report-Test: UriScan:(767451399110); X-Exchange-Antispam-Report-CFA-Test: BCL:0;PCL:0;RULEID:(100000700101)(100105000095)(100000701101)(100105300095)(100000702101)(100105100095)(6040450)(601004)(2401047)(8121501046)(5005006)(93006095)(93001095)(10201501046)(3002001)(100000703101)(100105400095)(6055026)(6041248)(20161123558100)(201703131423075)(201702281528075)(201703061421075)(201703061406153)(20161123564025)(20161123560025)(20161123555025)(20161123562025)(6072148)(100000704101)(100105200095)(100000705101)(100105500095);SRVR:MWHPR12MB1152;BCL:0;PCL:0;RULEID:(100000800101)(100110000095)(100000801101)(100110300095)(100000802101)(100110100095)(100000803101)(100110400095)(100000804101)(100110200095)(100000805101)(100110500095);SRVR:MWHPR12MB1152; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtNV0hQUjEyTUIxMTUyOzQ6MCt3QXhkUElweGpQclFzQTN0cmVkTmUyZ3RW?= =?utf-8?B?MTMvTTdTcGVjdThCTXJrRDYza0F6NTdqaFJHVFdZZUxsTGdadnZrc0kzUVRL?= =?utf-8?B?eG1EcHBEblNRRmlRK2JWMHFtOEVpYTBPMjZXYjczcVd0cVZ6WURGdDlwL21a?= =?utf-8?B?M2ZlSms5MDJweThZYkQyZUNkZDRWRDhicGNMYWJhNWx2VmV6d05Dc25OeCtQ?= =?utf-8?B?OWxrZlZTY0ZDWkI5aGc0YzJHOU4wejg0QnpUR1ppcDZkYUdTWlhWcld1ZUxL?= =?utf-8?B?WGVwaGFHU1EzQm9iOWFEOUhUc0FuSm1pK0NiVUtqTUh2WEN4RS9JTVJZVnUx?= =?utf-8?B?MzBnZGhKMmgvQUgzelVocDVBL0dBSjZvZDFZYzVwYzFBbXVIODJyWkI5dkJm?= =?utf-8?B?V3M0VHV2ZHhDK0FBUVFwR3hTN1Z0WnZ5K1pZVTNUSHpEMm1vZytvc0g3NDYr?= =?utf-8?B?VnZuSk1CNFp3TExNa0tTakVqd01ma1RVc25tNFZBNTdJampBWEV0dmNRYWwz?= =?utf-8?B?UGdCZlpYcFBwY0l1WG9QTWJ4V1NrTGdSMTJhTmFNTzV3cTVUN2JWTFBKY3dS?= =?utf-8?B?YWV3cnZHejlVcmlFVFJlODZLQnozOUs4MllHMHEyVkhMU0k3d05qa2NOQVdH?= =?utf-8?B?Ni9LUHgwR0czOHFLWmFmOGhlbVQ3UG94OVUxZXpwV1VsM2ZzanEzUVZ4YVpq?= =?utf-8?B?TWlDZ2haajJNRmN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?ZRCJja3ipr2hGhXedmh?= X-Forefront-PRVS: 03319F6FEF X-Forefront-Antispam-Report: SFV:NSPM;SFS:(10009020)(4630300001)(6009001)(39450400003)(39840400002)(39400400002)(39860400002)(39850400002)(39410400002)(81166006)(4001350100001)(7406005)(50986999)(54356999)(8676002)(76176999)(305945005)(47776003)(66066001)(7416002)(5660300001)(23676002)(42186005)(50466002)(33646002)(7736002)(53416004)(2906002)(6666003)(53936002)(103116003)(54906002)(86362001)(9686003)(55016002)(2950100002)(478600001)(6116002)(3846002)(4326008)(1076002)(230700001)(25786009)(6506006)(189998001)(97746001)(83506001)(110136004)(38730400002)(72206003)(921003)(1121003);DIR:OUT;SFP:1101;SCL:1;SRVR:MWHPR12MB1152;H:tlendack-t1.amdoffice.net;FPR:;SPF:None;MLV:sfv;LANG:en; X-Microsoft-Exchange-Diagnostics: =?utf-8?B?MTtNV0hQUjEyTUIxMTUyOzIzOjRpVGVxUUdPbGNvemdEZkZaUkUzVURxSktk?= =?utf-8?B?UGVhY25hcHF5clM5aXRYdXZNMnBGdGx6SnpxMWNNRjY0a1Nqc0E1SGlQUTZs?= =?utf-8?B?N1ZDSHlzODZoa3pXYzNSeVRRL0VmOFRydFJ1cGFDYmcwSjNOd1djRlMzK3Jr?= =?utf-8?B?WklFSGJGOW1tZkI3V3Q3UzNEdEpiTXJMZGRRZWg2WkZERFROUXJuOXJGVlM1?= =?utf-8?B?b1FpSUJwTUhlUUpHZkVoa1pyMkJ2OVY2YS9KT2VpWjNBRFhnQ2ltSkg4NkF6?= =?utf-8?B?NnZMamduZEdIV3E2MmVNQ0IveEZ1YklXK2l6K084TmFycmhiL29uakVYNkhX?= =?utf-8?B?MWZSVUpNVnF3cSt0WGhQQnZER2V1VUFJV1c2ZVNYaGpJSFZLYjR2MEFoalpS?= =?utf-8?B?S25WRXJoeGZtd2tBN2lmVGd6d2JvNkR3NjlZdE5YRzFHelpMaStyT0NxVjFF?= =?utf-8?B?Q2JUZFlTaUtXekNvVFpjczN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Y5kQnRGZNtWhzP2jTTIpkCDXuvvPCPVBKaAyYDUAp5oVzzJR7vogvtBImVNZCKZgYxDYwuHIhHD/OlQMHUF+fwM14sL/Vbyla4bC2zRdigINDaYG/22ZkgiKZ9lyWEhP51E5kSJJLG4+GBGK5HVy5oeyO3JlMbPQxkuYwADKC7ZB6X0x82OhX8Zs3u X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 07 Jun 2017 19:19:08.0888 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-Transport-CrossTenantHeadersStamped: MWHPR12MB1152 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Length: 3795 Lines: 140 Add a cmdline_find_option() function to look for cmdline options that take arguments. The argument is returned in a supplied buffer and the argument length (regardless of whether it fits in the supplied buffer) is returned, with -1 indicating not found. Signed-off-by: Tom Lendacky --- arch/x86/include/asm/cmdline.h | 2 + arch/x86/lib/cmdline.c | 105 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) diff --git a/arch/x86/include/asm/cmdline.h b/arch/x86/include/asm/cmdline.h index e01f7f7..84ae170 100644 --- a/arch/x86/include/asm/cmdline.h +++ b/arch/x86/include/asm/cmdline.h @@ -2,5 +2,7 @@ #define _ASM_X86_CMDLINE_H int cmdline_find_option_bool(const char *cmdline_ptr, const char *option); +int cmdline_find_option(const char *cmdline_ptr, const char *option, + char *buffer, int bufsize); #endif /* _ASM_X86_CMDLINE_H */ diff --git a/arch/x86/lib/cmdline.c b/arch/x86/lib/cmdline.c index 5cc78bf..3261abb 100644 --- a/arch/x86/lib/cmdline.c +++ b/arch/x86/lib/cmdline.c @@ -104,7 +104,112 @@ static inline int myisspace(u8 c) return 0; /* Buffer overrun */ } +/* + * Find a non-boolean option (i.e. option=argument). In accordance with + * standard Linux practice, if this option is repeated, this returns the + * last instance on the command line. + * + * @cmdline: the cmdline string + * @max_cmdline_size: the maximum size of cmdline + * @option: option string to look for + * @buffer: memory buffer to return the option argument + * @bufsize: size of the supplied memory buffer + * + * Returns the length of the argument (regardless of if it was + * truncated to fit in the buffer), or -1 on not found. + */ +static int +__cmdline_find_option(const char *cmdline, int max_cmdline_size, + const char *option, char *buffer, int bufsize) +{ + char c; + int pos = 0, len = -1; + const char *opptr = NULL; + char *bufptr = buffer; + enum { + st_wordstart = 0, /* Start of word/after whitespace */ + st_wordcmp, /* Comparing this word */ + st_wordskip, /* Miscompare, skip */ + st_bufcpy, /* Copying this to buffer */ + } state = st_wordstart; + + if (!cmdline) + return -1; /* No command line */ + + /* + * This 'pos' check ensures we do not overrun + * a non-NULL-terminated 'cmdline' + */ + while (pos++ < max_cmdline_size) { + c = *(char *)cmdline++; + if (!c) + break; + + switch (state) { + case st_wordstart: + if (myisspace(c)) + break; + + state = st_wordcmp; + opptr = option; + /* fall through */ + + case st_wordcmp: + if ((c == '=') && !*opptr) { + /* + * We matched all the way to the end of the + * option we were looking for, prepare to + * copy the argument. + */ + len = 0; + bufptr = buffer; + state = st_bufcpy; + break; + } else if (c == *opptr++) { + /* + * We are currently matching, so continue + * to the next character on the cmdline. + */ + break; + } + state = st_wordskip; + /* fall through */ + + case st_wordskip: + if (myisspace(c)) + state = st_wordstart; + break; + + case st_bufcpy: + if (myisspace(c)) { + state = st_wordstart; + } else { + /* + * Increment len, but don't overrun the + * supplied buffer and leave room for the + * NULL terminator. + */ + if (++len < bufsize) + *bufptr++ = c; + } + break; + } + } + + if (bufsize) + *bufptr = '\0'; + + return len; +} + int cmdline_find_option_bool(const char *cmdline, const char *option) { return __cmdline_find_option_bool(cmdline, COMMAND_LINE_SIZE, option); } + +int cmdline_find_option(const char *cmdline, const char *option, char *buffer, + int bufsize) +{ + return __cmdline_find_option(cmdline, COMMAND_LINE_SIZE, option, + buffer, bufsize); +}