Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  * seq_buf.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  * Copyright (C) 2014 Red Hat Inc, Steven Rostedt <srostedt@redhat.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  * The seq_buf is a handy tool that allows you to pass a descriptor around
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8)  * to a buffer that other functions can write to. It is similar to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9)  * seq_file functionality but has some differences.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11)  * To use it, the seq_buf must be initialized with seq_buf_init().
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12)  * This will set up the counters within the descriptor. You can call
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13)  * seq_buf_init() more than once to reset the seq_buf to start
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14)  * from scratch.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/seq_file.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/seq_buf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21)  * seq_buf_can_fit - can the new data fit in the current buffer?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22)  * @s: the seq_buf descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23)  * @len: The length to see if it can fit in the current buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25)  * Returns true if there's enough unused space in the seq_buf buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * to fit the amount of new data according to @len.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) static bool seq_buf_can_fit(struct seq_buf *s, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	return s->len + len <= s->size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34)  * seq_buf_print_seq - move the contents of seq_buf into a seq_file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35)  * @m: the seq_file descriptor that is the destination
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36)  * @s: the seq_buf descriptor that is the source.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38)  * Returns zero on success, non zero otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) int seq_buf_print_seq(struct seq_file *m, struct seq_buf *s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	unsigned int len = seq_buf_used(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	return seq_write(m, s->buffer, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * seq_buf_vprintf - sequence printing of information.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  * @s: seq_buf descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50)  * @fmt: printf format string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51)  * @args: va_list of arguments from a printf() type function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53)  * Writes a vnprintf() format into the sequencce buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55)  * Returns zero on success, -1 on overflow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) int seq_buf_vprintf(struct seq_buf *s, const char *fmt, va_list args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	WARN_ON(s->size == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	if (s->len < s->size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 		len = vsnprintf(s->buffer + s->len, s->size - s->len, fmt, args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 		if (s->len + len < s->size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 			s->len += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 	seq_buf_set_overflow(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75)  * seq_buf_printf - sequence printing of information
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76)  * @s: seq_buf descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77)  * @fmt: printf format string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79)  * Writes a printf() format into the sequence buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81)  * Returns zero on success, -1 on overflow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) int seq_buf_printf(struct seq_buf *s, const char *fmt, ...)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 	va_list ap;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	va_start(ap, fmt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 	ret = seq_buf_vprintf(s, fmt, ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 	va_end(ap);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) EXPORT_SYMBOL_GPL(seq_buf_printf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) #ifdef CONFIG_BINARY_PRINTF
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98)  * seq_buf_bprintf - Write the printf string from binary arguments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99)  * @s: seq_buf descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100)  * @fmt: The format string for the @binary arguments
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)  * @binary: The binary arguments for @fmt.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103)  * When recording in a fast path, a printf may be recorded with just
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)  * saving the format and the arguments as they were passed to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)  * function, instead of wasting cycles converting the arguments into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)  * ASCII characters. Instead, the arguments are saved in a 32 bit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)  * word array that is defined by the format string constraints.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)  * This function will take the format and the binary array and finish
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)  * the conversion into the ASCII string within the buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112)  * Returns zero on success, -1 on overflow.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) int seq_buf_bprintf(struct seq_buf *s, const char *fmt, const u32 *binary)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 	unsigned int len = seq_buf_buffer_left(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 	WARN_ON(s->size == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	if (s->len < s->size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		ret = bstr_printf(s->buffer + s->len, len, fmt, binary);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 		if (s->len + ret < s->size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 			s->len += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 			return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 	seq_buf_set_overflow(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) #endif /* CONFIG_BINARY_PRINTF */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)  * seq_buf_puts - sequence printing of simple string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135)  * @s: seq_buf descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136)  * @str: simple string to record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)  * Copy a simple string into the sequence buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)  * Returns zero on success, -1 on overflow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) int seq_buf_puts(struct seq_buf *s, const char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 	size_t len = strlen(str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 	WARN_ON(s->size == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 	/* Add 1 to len for the trailing null byte which must be there */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	len += 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 	if (seq_buf_can_fit(s, len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 		memcpy(s->buffer + s->len, str, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) 		/* Don't count the trailing null byte against the capacity */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 		s->len += len - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) 	seq_buf_set_overflow(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162)  * seq_buf_putc - sequence printing of simple character
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)  * @s: seq_buf descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)  * @c: simple character to record
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)  * Copy a single character into the sequence buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)  * Returns zero on success, -1 on overflow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) int seq_buf_putc(struct seq_buf *s, unsigned char c)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 	WARN_ON(s->size == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 	if (seq_buf_can_fit(s, 1)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 		s->buffer[s->len++] = c;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) 	seq_buf_set_overflow(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)  * seq_buf_putmem - write raw data into the sequenc buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)  * @s: seq_buf descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185)  * @mem: The raw memory to copy into the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)  * @len: The length of the raw memory to copy (in bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188)  * There may be cases where raw memory needs to be written into the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)  * buffer and a strcpy() would not work. Using this function allows
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190)  * for such cases.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192)  * Returns zero on success, -1 on overflow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) int seq_buf_putmem(struct seq_buf *s, const void *mem, unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	WARN_ON(s->size == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	if (seq_buf_can_fit(s, len)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		memcpy(s->buffer + s->len, mem, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 		s->len += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	seq_buf_set_overflow(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 	return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) #define MAX_MEMHEX_BYTES	8U
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) #define HEX_CHARS		(MAX_MEMHEX_BYTES*2 + 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)  * seq_buf_putmem_hex - write raw memory into the buffer in ASCII hex
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212)  * @s: seq_buf descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)  * @mem: The raw memory to write its hex ASCII representation of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214)  * @len: The length of the raw memory to copy (in bytes)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216)  * This is similar to seq_buf_putmem() except instead of just copying the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)  * raw memory into the buffer it writes its ASCII representation of it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)  * in hex characters.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)  * Returns zero on success, -1 on overflow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) int seq_buf_putmem_hex(struct seq_buf *s, const void *mem,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) 		       unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) 	unsigned char hex[HEX_CHARS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) 	const unsigned char *data = mem;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) 	unsigned int start_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) 	int i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) 	WARN_ON(s->size == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) 	BUILD_BUG_ON(MAX_MEMHEX_BYTES * 2 >= HEX_CHARS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) 	while (len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) 		start_len = min(len, MAX_MEMHEX_BYTES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) #ifdef __BIG_ENDIAN
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) 		for (i = 0, j = 0; i < start_len; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) #else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) 		for (i = start_len-1, j = 0; i >= 0; i--) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 			hex[j++] = hex_asc_hi(data[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 			hex[j++] = hex_asc_lo(data[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 		if (WARN_ON_ONCE(j == 0 || j/2 > len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 		/* j increments twice per loop */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 		hex[j++] = ' ';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 		seq_buf_putmem(s, hex, j);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 		if (seq_buf_has_overflowed(s))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 			return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 		len -= start_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 		data += start_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261)  * seq_buf_path - copy a path into the sequence buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)  * @s: seq_buf descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263)  * @path: path to write into the sequence buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)  * @esc: set of characters to escape in the output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266)  * Write a path name into the sequence buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)  * Returns the number of written bytes on success, -1 on overflow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) int seq_buf_path(struct seq_buf *s, const struct path *path, const char *esc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 	char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	size_t size = seq_buf_get_buf(s, &buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 	int res = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 	WARN_ON(s->size == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 	if (size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		char *p = d_path(path, buf, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 		if (!IS_ERR(p)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 			char *end = mangle_path(buf, p, esc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 			if (end)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 				res = end - buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) 	seq_buf_commit(s, res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) 	return res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292)  * seq_buf_to_user - copy the squence buffer to user space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)  * @s: seq_buf descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294)  * @ubuf: The userspace memory location to copy to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)  * @cnt: The amount to copy
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297)  * Copies the sequence buffer into the userspace memory pointed to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298)  * by @ubuf. It starts from the last read position (@s->readpos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299)  * and writes up to @cnt characters or till it reaches the end of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)  * the content in the buffer (@s->len), which ever comes first.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)  * On success, it returns a positive number of the number of bytes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303)  * it copied.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)  * On failure it returns -EBUSY if all of the content in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306)  * sequence has been already read, which includes nothing in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307)  * sequence (@s->len == @s->readpos).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)  * Returns -EFAULT if the copy to userspace fails.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) int seq_buf_to_user(struct seq_buf *s, char __user *ubuf, int cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 	int len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 	if (!cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 	len = seq_buf_used(s);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 	if (len <= s->readpos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 		return -EBUSY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 	len -= s->readpos;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	if (cnt > len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 		cnt = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) 	ret = copy_to_user(ubuf, s->buffer + s->readpos, cnt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) 	if (ret == cnt)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) 	cnt -= ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) 	s->readpos += cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) 	return cnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)  * seq_buf_hex_dump - print formatted hex dump into the sequence buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339)  * @s: seq_buf descriptor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340)  * @prefix_str: string to prefix each line with;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341)  *  caller supplies trailing spaces for alignment if desired
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342)  * @prefix_type: controls whether prefix of an offset, address, or none
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343)  *  is printed (%DUMP_PREFIX_OFFSET, %DUMP_PREFIX_ADDRESS, %DUMP_PREFIX_NONE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)  * @rowsize: number of bytes to print per line; must be 16 or 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345)  * @groupsize: number of bytes to print at a time (1, 2, 4, 8; default = 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346)  * @buf: data blob to dump
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347)  * @len: number of bytes in the @buf
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348)  * @ascii: include ASCII after the hex output
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350)  * Function is an analogue of print_hex_dump() and thus has similar interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)  * linebuf size is maximal length for one line.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353)  * 32 * 3 - maximum bytes per line, each printed into 2 chars + 1 for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354)  *	separating space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)  * 2 - spaces separating hex dump and ascii representation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356)  * 32 - ascii representation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)  * 1 - terminating '\0'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359)  * Returns zero on success, -1 on overflow
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) int seq_buf_hex_dump(struct seq_buf *s, const char *prefix_str, int prefix_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) 		     int rowsize, int groupsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) 		     const void *buf, size_t len, bool ascii)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) 	const u8 *ptr = buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) 	int i, linelen, remaining = len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) 	unsigned char linebuf[32 * 3 + 2 + 32 + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) 	if (rowsize != 16 && rowsize != 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) 		rowsize = 16;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) 	for (i = 0; i < len; i += rowsize) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) 		linelen = min(remaining, rowsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) 		remaining -= rowsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) 		hex_dump_to_buffer(ptr + i, linelen, rowsize, groupsize,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) 				   linebuf, sizeof(linebuf), ascii);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) 		switch (prefix_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) 		case DUMP_PREFIX_ADDRESS:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) 			ret = seq_buf_printf(s, "%s%p: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) 			       prefix_str, ptr + i, linebuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) 		case DUMP_PREFIX_OFFSET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) 			ret = seq_buf_printf(s, "%s%.8x: %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) 					     prefix_str, i, linebuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) 			ret = seq_buf_printf(s, "%s%s\n", prefix_str, linebuf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) 		if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) }