blob: 4edb3ea6e6aedd559657acb24c71b4a691b9daf3 [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 * Copyright (C) 2009 coresystems GmbH
Eric Biederman8ca8d762003-04-22 19:02:15 +00005 *
Stefan Reinauer52fc6b12009-10-24 13:06:04 +00006 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; version 2 of
9 * the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Eric Biederman8ca8d762003-04-22 19:02:15 +000015 */
16
Stefan Reinauer52fc6b12009-10-24 13:06:04 +000017#include <console/vtxprintf.h>
Edward O'Callaghan0ddb8262014-06-17 18:37:08 +100018#include <string.h>
Rudolf Marek7f0e9302011-09-02 23:23:41 +020019#include <trace.h>
Eric Biederman8ca8d762003-04-22 19:02:15 +000020
Vladimir Serbinenko4b5012a2013-11-26 22:07:47 +010021struct vsnprintf_context
Eric Biederman8ca8d762003-04-22 19:02:15 +000022{
Vladimir Serbinenko22676582013-11-26 21:48:57 +010023 char *str_buf;
Vladimir Serbinenko4b5012a2013-11-26 22:07:47 +010024 size_t buf_limit;
Vladimir Serbinenko22676582013-11-26 21:48:57 +010025};
26
27static void str_tx_byte(unsigned char byte, void *data)
28{
Vladimir Serbinenko4b5012a2013-11-26 22:07:47 +010029 struct vsnprintf_context *ctx = data;
30 if (ctx->buf_limit) {
31 *ctx->str_buf = byte;
32 ctx->str_buf++;
33 ctx->buf_limit--;
34 }
Stefan Reinauer52fc6b12009-10-24 13:06:04 +000035}
Patrick Georgi736221f12009-05-26 14:31:37 +000036
Vladimir Serbinenko4b5012a2013-11-26 22:07:47 +010037static int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
Stefan Reinauer52fc6b12009-10-24 13:06:04 +000038{
Eric Biederman8ca8d762003-04-22 19:02:15 +000039 int i;
Vladimir Serbinenko4b5012a2013-11-26 22:07:47 +010040 struct vsnprintf_context ctx;
Stefan Reinauer52fc6b12009-10-24 13:06:04 +000041
Rudolf Marek7f0e9302011-09-02 23:23:41 +020042 DISABLE_TRACE;
Stefan Reinauer52fc6b12009-10-24 13:06:04 +000043
Vladimir Serbinenko22676582013-11-26 21:48:57 +010044 ctx.str_buf = buf;
Vladimir Serbinenko4b5012a2013-11-26 22:07:47 +010045 ctx.buf_limit = size ? size - 1 : 0;
Kyösti Mälkkib04e0ff2014-02-04 14:28:17 +020046 i = vtxprintf(str_tx_byte, fmt, args, &ctx);
Vladimir Serbinenko4b5012a2013-11-26 22:07:47 +010047 if (size)
48 *ctx.str_buf = '\0';
Stefan Reinauer52fc6b12009-10-24 13:06:04 +000049
Rudolf Marek7f0e9302011-09-02 23:23:41 +020050 ENABLE_TRACE;
Stefan Reinauer52fc6b12009-10-24 13:06:04 +000051
Eric Biederman8ca8d762003-04-22 19:02:15 +000052 return i;
53}
54
Vladimir Serbinenko4b5012a2013-11-26 22:07:47 +010055int snprintf(char *buf, size_t size, const char *fmt, ...)
56{
57 va_list args;
58 int i;
59
60 va_start(args, fmt);
61 i = vsnprintf(buf, size, fmt, args);
Eric Biederman8ca8d762003-04-22 19:02:15 +000062 va_end(args);
Stefan Reinauer52fc6b12009-10-24 13:06:04 +000063
Eric Biederman8ca8d762003-04-22 19:02:15 +000064 return i;
65}