summaryrefslogtreecommitdiffstats
path: root/lib/show_progress.c
blob: 1b624bcb9af8053f18363bc1c06c77cab068535b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * show_progress.c - simple progress bar functions
 *
 * Copyright (c) 2010 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#include <common.h>
#include <fs.h>
#include <progress.h>
#include <linux/math64.h>

#define HASHES_PER_LINE	64

static loff_t printed;
static loff_t progress_max;
static unsigned spin;

void show_progress(loff_t now)
{
	char spinchr[] = "\\|/-";

	if (now < 0) {
		printf("%c\b", spinchr[spin++ % (sizeof(spinchr) - 1)]);
		return;
	}

	if (progress_max && progress_max != FILESIZE_MAX) {
		uint64_t tmp = now * HASHES_PER_LINE;
		now = div64_u64(tmp, progress_max);
	}

	while (printed < now) {
		if (!(printed % HASHES_PER_LINE) && printed)
			printf("\n\t");
		printf("#");
		printed++;
	}
}

void init_progression_bar(loff_t max)
{
	printed = 0;
	progress_max = max;
	spin = 0;
	if (progress_max && progress_max != FILESIZE_MAX)
		printf("\t[%*s]\r\t[", HASHES_PER_LINE, "");
	else
		printf("\t");
}

NOTIFIER_HEAD(progress_notifier_list);

static int progress_logger(struct notifier_block *r, unsigned long stage, void *_prefix)
{
	const char *prefix = _prefix;

	switch ((enum progress_stage)stage) {
		case PROGRESS_UPDATING:
			pr_info("%sSoftware update in progress\n", prefix);
			break;
		case PROGRESS_UPDATE_SUCCESS:
			pr_info("%sSoftware update finished successfully\n", prefix);
			break;
		case PROGRESS_UPDATE_FAIL:
			pr_info("%sSoftware update failed\n", prefix);
			break;
		case PROGRESS_UNSPECIFIED:
			/* default state. This should not be reached */
			break;
	}

	return 0;
}

struct notifier_block progress_log_client =  {
	.notifier_call = progress_logger
};