From patchwork Thu Nov 10 13:09:29 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Evgeniy Baskov X-Patchwork-Id: 18122 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp137825wru; Thu, 10 Nov 2022 05:10:13 -0800 (PST) X-Google-Smtp-Source: AMsMyM7IoqjFywLC10/FmobLCaii/Lj4HJxPqxfnpIT5j5kw0x+T56Eq2JgfpHsYUlAmmPRlywI9 X-Received: by 2002:a17:902:f546:b0:186:c6d7:6556 with SMTP id h6-20020a170902f54600b00186c6d76556mr63659920plf.109.1668085813134; Thu, 10 Nov 2022 05:10:13 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668085813; cv=none; d=google.com; s=arc-20160816; b=R3Y0CL7+TaSmJMoK0azXsbFF6HuqOnpYW/yZRhVMq5K+u3SaRVDyYLFHEY8P/lhX/G hhKNTMEso0zNaHT91j4hF6e6uitHstVoAE0Ej3GjltzX2E07GvoKNDSlX0bUSbxR0xh2 RaliQlrXtbEN/tbsB+forADwhLs5+wym5Yr3uI/c2mXdkpm2b0wdKIkI7m77vse4VrqR 7V5H+s+ol49Gpw1RuYESAMjxfd+Agt4PbAz0L3QMjm6DUPJZxWfE5c0u2xdj36VmaSR8 jA/poAPHxGidhFYarBfXtyWBvnJfr/VeW2aPXSL3aHc92gerIPjWyNCvP3n57kTcCXps oWmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature:dkim-filter; bh=LhB34PvVQmEmAHSgcIjFt8cb9vXQe9ZNmveaYWGCLFg=; b=oAn/mH61QQ8eVEIUdqeIDeI10J07fnDkJnhdCj5mXqb3hROO+OmdeCWokES0FFkTWz 5DBOHHbrxkwadcb/mtPnsN2PAx/8OcTXpytDTmf7CiGVmSCqVqyjLUun+PhLRjf73h08 w0EUg9z8gi7BFnedTSZDd5TkrBxCgp6+7r1abZKz/tY/3ONetvEz8D5ga//YYSjYskdg FyiywK3UWu1aD4TURcyilRzuuJ+07tLEZJ3d+220dNkSc3dLerNO4800j7+irL6yMQ33 7V5YY2xFqv3pWzfqni8LnfnbOZLqM+z1U3yQefPqJDAE1NbaEIXj1amwwL2fi30aYjf/ 6oTQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@ispras.ru header.s=default header.b=JotLW5cT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ispras.ru Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 22-20020a631356000000b0046fe2432570si20484904pgt.390.2022.11.10.05.09.56; Thu, 10 Nov 2022 05:10:13 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@ispras.ru header.s=default header.b=JotLW5cT; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=ispras.ru Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230291AbiKJNJo (ORCPT + 99 others); Thu, 10 Nov 2022 08:09:44 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35390 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229602AbiKJNJj (ORCPT ); Thu, 10 Nov 2022 08:09:39 -0500 Received: from mail.ispras.ru (mail.ispras.ru [83.149.199.84]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0278212AA2 for ; Thu, 10 Nov 2022 05:09:37 -0800 (PST) Received: from localhost.localdomain (unknown [83.149.199.65]) by mail.ispras.ru (Postfix) with ESMTPSA id 8DCEC419E9E9; Thu, 10 Nov 2022 13:09:35 +0000 (UTC) DKIM-Filter: OpenDKIM Filter v2.11.0 mail.ispras.ru 8DCEC419E9E9 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ispras.ru; s=default; t=1668085775; bh=LhB34PvVQmEmAHSgcIjFt8cb9vXQe9ZNmveaYWGCLFg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JotLW5cTg/KfF0EyW60fIU3JsvmBWiXWZVTRPATEISShL6URLYSSH6Eh3DAyoKR3B fN9uT9GOp4ooNWOW/EJrjdQsIiqsim/sE+pYDRYdb/z/7ow2L2EvYvJ1C4/IpAQ6Kq UwATpAahCAdspwlzdZhwjjWFQ+rIVUvjomPsZnL4= From: Evgeniy Baskov To: Borislav Petkov Cc: Evgeniy Baskov , Dave Hansen , Ingo Molnar , Thomas Gleixner , linux-kernel@vger.kernel.org, x86@kernel.org, Alexey Khoroshilov Subject: [PATCH v8 1/5] x86/boot: Add strlcat() and strscpy() to compressed kernel Date: Thu, 10 Nov 2022 16:09:29 +0300 Message-Id: X-Mailer: git-send-email 2.37.4 In-Reply-To: References: MIME-Version: 1.0 X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749114749322723592?= X-GMAIL-MSGID: =?utf-8?q?1749114749322723592?= These functions simplify the code of command line concatenation helper and reduce the probability of mistakes. Use simpler implementation of strscpy than used in it kernel itself to avoid code bloat in compressed kernel. Signed-off-by: Evgeniy Baskov --- arch/x86/boot/compressed/string.c | 50 +++++++++++++++++++++++++++++++ arch/x86/purgatory/purgatory.c | 1 + 2 files changed, 51 insertions(+) diff --git a/arch/x86/boot/compressed/string.c b/arch/x86/boot/compressed/string.c index 81fc1eaa3229..5c193fa0a09b 100644 --- a/arch/x86/boot/compressed/string.c +++ b/arch/x86/boot/compressed/string.c @@ -40,6 +40,56 @@ static void *____memcpy(void *dest, const void *src, size_t n) } #endif +size_t strlcat(char *dest, const char *src, size_t count) +{ + size_t dsize = strlen(dest); + size_t len = strlen(src); + size_t res = dsize + len; + + /* This would be a bug */ + if (dsize >= count) + error("strlcat(): destination too big\n"); + + dest += dsize; + count -= dsize; + if (len >= count) + len = count-1; + memcpy(dest, src, len); + dest[len] = 0; + return res; +} + +/* Don't include word-at-a-time code path in compressed kernel for simplicity */ +size_t strscpy(char *dest, const char *src, size_t count) +{ + long res = 0; + + if (count == 0) + return -E2BIG; + + if (count > INT_MAX) { + warn("strscpy(): Count is too big"); + return -E2BIG; + } + + while (count) { + char c; + + c = src[res]; + dest[res] = c; + if (!c) + return res; + res++; + count--; + } + + /* Hit buffer length without finding a NUL; force NUL-termination. */ + if (res) + dest[res-1] = '\0'; + + return -E2BIG; +} + void *memset(void *s, int c, size_t n) { int i; diff --git a/arch/x86/purgatory/purgatory.c b/arch/x86/purgatory/purgatory.c index 7558139920f8..65f0cedb65ae 100644 --- a/arch/x86/purgatory/purgatory.c +++ b/arch/x86/purgatory/purgatory.c @@ -57,3 +57,4 @@ void purgatory(void) * arch/x86/boot/compressed/string.c */ void warn(const char *msg) {} +void error(char *m) {}