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) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3) #include <lzma.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5) #include <linux/compiler.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) #include <fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #include "compress.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <internal/lib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #define BUFSIZE 8192
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) static const char *lzma_strerror(lzma_ret ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 	switch ((int) ret) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) 	case LZMA_MEM_ERROR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) 		return "Memory allocation failed";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) 	case LZMA_OPTIONS_ERROR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) 		return "Unsupported decompressor flags";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 	case LZMA_FORMAT_ERROR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) 		return "The input is not in the .xz format";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26) 	case LZMA_DATA_ERROR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27) 		return "Compressed file is corrupt";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28) 	case LZMA_BUF_ERROR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29) 		return "Compressed file is truncated or otherwise corrupt";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) 		return "Unknown error, possibly a bug";
^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) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) int lzma_decompress_to_file(const char *input, int output_fd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 	lzma_action action = LZMA_RUN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) 	lzma_stream strm   = LZMA_STREAM_INIT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) 	lzma_ret ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 	int err = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) 	u8 buf_in[BUFSIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) 	u8 buf_out[BUFSIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 	FILE *infile;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46) 	infile = fopen(input, "rb");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47) 	if (!infile) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48) 		pr_err("lzma: fopen failed on %s: '%s'\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49) 		       input, strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	ret = lzma_stream_decoder(&strm, UINT64_MAX, LZMA_CONCATENATED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 	if (ret != LZMA_OK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		pr_err("lzma: lzma_stream_decoder failed %s (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 			lzma_strerror(ret), ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 		goto err_fclose;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	strm.next_in   = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	strm.avail_in  = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	strm.next_out  = buf_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	strm.avail_out = sizeof(buf_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	while (1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 		if (strm.avail_in == 0 && !feof(infile)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 			strm.next_in  = buf_in;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 			strm.avail_in = fread(buf_in, 1, sizeof(buf_in), infile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 			if (ferror(infile)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 				pr_err("lzma: read error: %s\n", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 				goto err_lzma_end;
^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) 			if (feof(infile))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 				action = LZMA_FINISH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		ret = lzma_code(&strm, action);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		if (strm.avail_out == 0 || ret == LZMA_STREAM_END) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 			ssize_t write_size = sizeof(buf_out) - strm.avail_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 			if (writen(output_fd, buf_out, write_size) != write_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 				pr_err("lzma: write error: %s\n", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 				goto err_lzma_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 			strm.next_out  = buf_out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 			strm.avail_out = sizeof(buf_out);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 		if (ret != LZMA_OK) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 			if (ret == LZMA_STREAM_END)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 			pr_err("lzma: failed %s\n", lzma_strerror(ret));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 			goto err_lzma_end;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) err_lzma_end:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 	lzma_end(&strm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) err_fclose:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) 	fclose(infile);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) bool lzma_is_compressed(const char *input)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) 	int fd = open(input, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) 	const uint8_t magic[6] = { 0xFD, '7', 'z', 'X', 'Z', 0x00 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	char buf[6] = { 0 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	ssize_t rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	if (fd < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 		return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	rc = read(fd, buf, sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 	close(fd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 	return rc == sizeof(buf) ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 	       memcmp(buf, magic, sizeof(buf)) == 0 : false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }