blob: 78bc09f5a76a667ed3b347b57213a2a4f95a016a [file] [log] [blame]
Eric Biederman8ca8d762003-04-22 19:02:15 +00001/*
Stefan Reinauer52fc6b12009-10-24 13:06:04 +00002 * This file is part of the coreboot project.
Stefan Reinauer14e22772010-04-27 06:56:47 +00003 *
Stefan Reinauer52fc6b12009-10-24 13:06:04 +00004 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; version 2 of
7 * the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Eric Biederman8ca8d762003-04-22 19:02:15 +000013 */
14
Stefan Reinauer52fc6b12009-10-24 13:06:04 +000015#include <console/vtxprintf.h>
Edward O'Callaghan0ddb8262014-06-17 18:37:08 +100016#include <string.h>
Rudolf Marek7f0e9302011-09-02 23:23:41 +020017#include <trace.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000018
Elyes HAOUASa4184142018-05-15 20:57:01 +020019struct vsnprintf_context {
Vladimir Serbinenko22676582013-11-26 21:48:57 +010020 char *str_buf;
Vladimir Serbinenko4b5012a2013-11-26 22:07:47 +010021 size_t buf_limit;
Vladimir Serbinenko22676582013-11-26 21:48:57 +010022};
23
24static void str_tx_byte(unsigned char byte, void *data)
25{
Vladimir Serbinenko4b5012a2013-11-26 22:07:47 +010026 struct vsnprintf_context *ctx = data;
27 if (ctx->buf_limit) {
28 *ctx->str_buf = byte;
29 ctx->str_buf++;
30 ctx->buf_limit--;
31 }
Stefan Reinauer52fc6b12009-10-24 13:06:04 +000032}
Patrick Georgi736221f12009-05-26 14:31:37 +000033
David Hendricks6053a9c2018-01-28 18:01:10 -080034int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
Stefan Reinauer52fc6b12009-10-24 13:06:04 +000035{
Eric Biederman8ca8d762003-04-22 19:02:15 +000036 int i;
Vladimir Serbinenko4b5012a2013-11-26 22:07:47 +010037 struct vsnprintf_context ctx;
Stefan Reinauer52fc6b12009-10-24 13:06:04 +000038
Rudolf Marek7f0e9302011-09-02 23:23:41 +020039 DISABLE_TRACE;
Stefan Reinauer52fc6b12009-10-24 13:06:04 +000040
Vladimir Serbinenko22676582013-11-26 21:48:57 +010041 ctx.str_buf = buf;
Vladimir Serbinenko4b5012a2013-11-26 22:07:47 +010042 ctx.buf_limit = size ? size - 1 : 0;
Kyösti Mälkkib04e0ff2014-02-04 14:28:17 +020043 i = vtxprintf(str_tx_byte, fmt, args, &ctx);
Vladimir Serbinenko4b5012a2013-11-26 22:07:47 +010044 if (size)
45 *ctx.str_buf = '\0';
Stefan Reinauer52fc6b12009-10-24 13:06:04 +000046
Rudolf Marek7f0e9302011-09-02 23:23:41 +020047 ENABLE_TRACE;
Stefan Reinauer52fc6b12009-10-24 13:06:04 +000048
Eric Biederman8ca8d762003-04-22 19:02:15 +000049 return i;
50}
51
Vladimir Serbinenko4b5012a2013-11-26 22:07:47 +010052int snprintf(char *buf, size_t size, const char *fmt, ...)
53{
54 va_list args;
55 int i;
56
57 va_start(args, fmt);
58 i = vsnprintf(buf, size, fmt, args);
Eric Biederman8ca8d762003-04-22 19:02:15 +000059 va_end(args);
Stefan Reinauer52fc6b12009-10-24 13:06:04 +000060
Eric Biederman8ca8d762003-04-22 19:02:15 +000061 return i;
62}