/* * Button Hotplug driver * * Copyright (C) 2008-2010 Gabor Juhos * * Based on the diag.c - GPIO interface driver for Broadcom boards * Copyright (C) 2006 Mike Baker , * Copyright (C) 2006-2007 Felix Fietkau * Copyright (C) 2008 Andy Boyett * * 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. */ #include #include #include #include #include #include #include #include #define DRV_NAME "button-hotplug" #define DRV_VERSION "0.4.1" #define DRV_DESC "Button Hotplug driver" #define BH_SKB_SIZE 2048 #define PFX DRV_NAME ": " #undef BH_DEBUG #ifdef BH_DEBUG #define BH_DBG(fmt, args...) printk(KERN_DEBUG "%s: " fmt, DRV_NAME, ##args ) #else #define BH_DBG(fmt, args...) do {} while (0) #endif #define BH_ERR(fmt, args...) printk(KERN_ERR "%s: " fmt, DRV_NAME, ##args ) #ifndef BIT_MASK #define BIT_MASK(nr) (1UL << ((nr) % BITS_PER_LONG)) #endif struct bh_priv { unsigned long *seen; struct input_handle handle; }; struct bh_event { const char *name; char *action; unsigned long seen; struct sk_buff *skb; struct work_struct work; }; struct bh_map { unsigned int code; const char *name; }; extern u64 uevent_next_seqnum(void); #define BH_MAP(_code, _name) \ { \ .code = (_code), \ .name = (_name), \ } static struct bh_map button_map[] = { BH_MAP(BTN_0, "BTN_0"), BH_MAP(BTN_1, "BTN_1"), BH_MAP(BTN_2, "BTN_2"), BH_MAP(BTN_3, "BTN_3"), BH_MAP(BTN_4, "BTN_4"), BH_MAP(BTN_5, "BTN_5"), BH_MAP(BTN_6, "BTN_6"), BH_MAP(BTN_7, "BTN_7"), BH_MAP(BTN_8, "BTN_8"), BH_MAP(BTN_9, "BTN_9"), BH_MAP(KEY_RESTART, "reset"), BH_MAP(KEY_POWER, "power"), BH_MAP(KEY_RFKILL, "rfkill"), BH_MAP(KEY_WPS_BUTTON, "wps"), BH_MAP(KEY_WIMAX, "wwan"), }; /* -------------------------------------------------------------------------*/ static int bh_event_add_var(struct bh_event *event, int argv, const char *format, ...) { static char buf[128]; char *s; va_list args; int len; if (argv) return 0; va_start(args, format); len = vsnprintf(buf, sizeof(buf), format, args); va_end(args); if (len >= sizeof(buf)) { BH_ERR("buffer size too small\n"); WARN_ON(1); return -ENOMEM; } s = skb_put(event->skb, len + 1); strcpy(s, buf); BH_DBG("added variable '%s'\n", s); return 0; } static int button_hotplug_fill_event(struct bh_event *event) { int ret; ret = bh_event_add_var(event, 0, "HOME=%s", "/"); if (ret) return ret; ret = bh_event_add_var(event, 0, "PATH=%s", "/sbin:/bin:/usr/sbin:/usr/bin"); if (ret) return ret; ret = bh_event_add_var(event, 0, "SUBSYSTEM=%s", "button"); if (ret) return ret; ret = bh_event_add_var(event, 0, "ACTION=%s", event->action); if (ret) return ret; ret = bh_event_add_var(event, 0, "BUTTON=%s", event->name); if (ret) return ret; ret = bh_event_add_var(event, 0, "SEEN=%ld", event->seen); if (ret) return ret; ret = bh_event_add_var(event, 0, "SEQNUM=%llu", uevent_next_seqnum()); return ret; } static void button_hotplug_work(struct work_struct *work) { struct bh_event *event = container_of(work, struct bh_event, work); int ret = 0; event->skb = alloc_skb(BH_SKB_SIZE, GFP_KERNEL); if (!event->skb) goto out_free_event; ret = bh_event_add_var(event, 0, "%s@", event->action); if (ret) goto out_free_skb; ret = button_hotplug_fill_event(event); if (ret) goto out_free_skb; NETLINK_CB(event->skb).dst_group = 1; broadcast_uevent(event->skb, 0, 1, GFP_KERNEL); out_free_skb: if (ret) { BH_ERR("work error %d\n", ret); kfree_skb(event->skb); } out_free_event: kfree(event); } static int button_hotplug_create_event(const char *name, unsigned long seen, int pressed) { struct bh_event *event; BH_DBG("create event, name=%s, seen=%lu, pressed=%d\n", name, seen, pressed); event = kzalloc(sizeof(*event), GFP_KERNEL); if (!event) return -ENOMEM; event->name = name; event->seen = seen; event->action = pressed ? "pressed" : "released"; INIT_WORK(&event->work, (void *)(void *)button_hotplug_work); schedule_work(&event->work); return 0; } /* -------------------------------------------------------------------------*/ static int button_get_index(unsigned int code) { int i; for (i = 0; i < ARRAY_SIZE(button_map); i++) if (button_map[i].code == code) return i; return -1; } static void button_hotplug_event(struct input_handle *handle, unsigned int type, unsigned int code, int value) { struct bh_priv *priv = handle->private; unsigned long seen = jiffies; int btn; BH_DBG("event type=%u, code=%u, value=%d\n", type, code, value); if (type != EV_KEY) return; btn = button_get_index(code); if (btn < 0) return; button_hotplug_create_event(button_map[btn].name, (seen - priv->seen[btn]) / HZ, value); priv->seen[btn] = seen; } static int button_hotplug_connect(struct input_handler *handler, struct input_dev *dev, const struct input_device_id *id) { struct bh_priv *priv; int ret; int i; for (i = 0; i < ARRAY_SIZE(button_map); i++) if (test_bit(button_map[i].code, dev->keybit)) break; if (i == ARRAY_SIZE(button_map)) return -ENODEV; priv = kzalloc(sizeof(*priv) + (sizeof(unsigned long) * ARRAY_SIZE(button_map)), GFP_KERNEL); if (!priv) return -ENOMEM; priv->seen = (unsigned long *) &priv[1]; priv->handle.private = priv; priv->handle.dev = dev; priv->handle.handler = handler; priv->handle.name = DRV_NAME; ret = input_register_handle(&priv->handle); if (ret) goto err_free_priv; ret = input_open_device(&priv->handle); if (ret) goto err_unregister_handle; BH_DBG("connected to %s\n", dev->name); return 0; err_unregister_handle: input_unregister_handle(&priv->handle); err_free_priv: kfree(priv); return ret; } static void button_hotplug_disconnect(struct input_handle *handle) { struct bh_priv *priv = handle->private; input_close_device(handle); input_unregister_handle(handle); kfree(priv); } static const struct input_device_id button_hotplug_ids[] = { { .flags = INPUT_DEVICE_ID_MATCH_EVBIT, .evbit = { BIT_MASK(EV_KEY) }, }, { /* Terminating entry */ }, }; MODULE_DEVICE_TABLE(input, button_hotplug_ids); static struct input_handler button_hotplug_handler = { .event = button_hotplug_event, .connect = button_hotplug_connect, .disconnect = button_hotplug_disconnect, .name = DRV_NAME, .id_table = button_hotplug_ids, }; /* -------------------------------------------------------------------------*/ static int __init button_hotplug_init(void) { int ret; printk(KERN_INFO DRV_DESC " version " DRV_VERSION "\n"); ret = input_register_handler(&button_hotplug_handler); if (ret) BH_ERR("unable to register input handler\n"); return ret; } module_init(button_hotplug_init); static void __exit button_hotplug_exit(void) { input_unregister_handler(&button_hotplug_handler); } module_exit(button_hotplug_exit); MODULE_DESCRIPTION(DRV_DESC); MODULE_VERSION(DRV_VERSION); MODULE_AUTHOR("Gabor Juhos "); MODULE_LICENSE("GPL v2"); href='#n141'>141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
/*
 * Builder/viewer/extractor utility for Poray firmware image files
 *
 * Copyright (C) 2013 Michel Stempin <michel.stempin@wanadoo.fr>
 * Copyright (C) 2013 Felix Kaechele <felix@fetzig.org>
 * Copyright (C) 2013 <admin@openschemes.com>
 *
 * This tool is based on:
 *   TP-Link firmware upgrade tool.
 *   Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
 *
 * Itself based on:
 *   TP-Link WR941 V2 firmware checksum fixing tool.
 *   Copyright (C) 2008,2009 Wang Jian <lark@linux.net.cn>
 *
 * 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.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include <getopt.h>
#include <stdarg.h>
#include <errno.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#if (__BYTE_ORDER == __BIG_ENDIAN)
#  define HOST_TO_BE32(x)	(x)
#  define BE32_TO_HOST(x)	(x)
#  define HOST_TO_LE32(x)	bswap_32(x)
#  define LE32_TO_HOST(x)	bswap_32(x)
#else
#  define HOST_TO_BE32(x)	bswap_32(x)
#  define BE32_TO_HOST(x)	bswap_32(x)
#  define HOST_TO_LE32(x)	(x)
#  define LE32_TO_HOST(x)	(x)
#endif

/* Fixed header flags */
#define HEADER_FLAGS		0x020e0000

/* Recognized Hardware ID magic */
#define HWID_HAME_MPR_A1_L8	0x32473352
#define HWID_PORAY_R50B		0x31353033
#define HWID_PORAY_R50D		0x33353033
#define HWID_PORAY_R50E		0x34353033
#define HWID_PORAY_M3		0x31353335
#define HWID_PORAY_M4		0x32353335
#define HWID_PORAY_Q3		0x33353335
#define HWID_PORAY_X5_X6	0x35353335
#define HWID_PORAY_X8		0x36353335
#define HWID_PORAY_X1		0x38353335
#define HWID_NEXX_WT1520	0x30353332
#define HWID_NEXX_WT3020	0x30323033
#define HWID_A5_V11		0x32473352

/* Recognized XOR obfuscation keys */
#define KEY_HAME		0
#define KEY_PORAY_1		1
#define KEY_PORAY_2		2
#define KEY_PORAY_3		3
#define KEY_PORAY_4		4
#define KEY_NEXX_1		5
#define KEY_NEXX_2		6
#define KEY_A5_V11		7

/* XOR key length */
#define KEY_LEN			15

struct file_info {
	char		*file_name;	/* Name of the file */
	uint32_t	file_size;	/* Length of the file */
};

struct fw_header {
	uint32_t	hw_id;		/* Hardware id */
	uint32_t	firmware_len;	/* Firmware data length */
	uint32_t	flags;		/* Header flags */
	uint8_t		pad[16];
} __attribute__ ((packed));

struct flash_layout {
	char		*id;
	uint32_t	fw_max_len;
};

struct board_info {
	char		*id;
	uint32_t	hw_id;
	char		*layout_id;
	uint32_t	key;
};

/*
 * Globals
 */
static char *ofname;
static char *progname;

static char *board_id;
static struct board_info *board;
static char *layout_id;
static struct flash_layout *layout;
static char *opt_hw_id;
static uint32_t hw_id;
static struct file_info firmware_info;
static uint32_t firmware_len = 0;

static int inspect = 0;
static int extract = 0;

static uint8_t key[][KEY_LEN] = {
  {0xC8, 0x3C, 0x3A, 0x93, 0xA2, 0x95, 0xC3, 0x63, 0x48, 0x45, 0x58, 0x09, 0x12, 0x03, 0x08},
  {0x89, 0x6B, 0x5A, 0x93, 0x92, 0x95, 0xC3, 0x63, 0xD0, 0xA3, 0x9C, 0x92, 0x2E, 0xE6, 0xC7},
  {0xC9, 0x1C, 0x3A, 0x93, 0x92, 0x95, 0xC3, 0x63, 0xD0, 0xA3, 0x9C, 0x92, 0x2E, 0xE6, 0xC7},
  {0x19, 0x1B, 0x3A, 0x93, 0x92, 0x95, 0xC3, 0x63, 0xD0, 0xA3, 0x9C, 0x92, 0x2E, 0xE6, 0xC7},
  {0x79, 0x7B, 0x7A, 0x93, 0x92, 0x95, 0xC3, 0x63, 0xD0, 0xA3, 0x9C, 0x92, 0x2E, 0xE6, 0xC7},
  {0x19, 0x1C, 0x4A, 0x93, 0x96, 0x95, 0xC3, 0x63, 0xD0, 0xA3, 0x9C, 0x92, 0x2E, 0x16, 0xC6},
  {0x39, 0x1C, 0x4A, 0x93, 0x96, 0x95, 0xC3, 0x63, 0xD0, 0xA3, 0x9C, 0x92, 0x2E, 0x16, 0xC6},
  {0xC8, 0x3C, 0x3A, 0x93, 0xA2, 0x95, 0xC3, 0x63, 0x48, 0x45, 0x58, 0x09, 0x20, 0x11, 0x08},
};

static struct flash_layout layouts[] = {
	{
		.id		= "4M",
		.fw_max_len	= 0x3c0000,
	}, {
		.id		= "8M",
		.fw_max_len	= 0x7c0000,
	}, {
		/* terminating entry */
	}
};

static struct board_info boards[] = {
	{
		.id             = "A5-V11",
		.hw_id          = HWID_A5_V11,
		.layout_id      = "4M",
		.key            = KEY_A5_V11,
        }, {
		.id		= "MPR-A1",
		.hw_id		= HWID_HAME_MPR_A1_L8,
		.layout_id	= "4M",
		.key		= KEY_HAME,
	}, {
		.id		= "MPR-L8",
		.hw_id		= HWID_HAME_MPR_A1_L8,
		.layout_id	= "4M",
		.key		= KEY_HAME,
	}, {
		.id		= "R50B",
		.hw_id		= HWID_PORAY_R50B,
		.layout_id	= "4M",
		.key		= KEY_PORAY_2,
	}, {
		.id		= "R50D",
		.hw_id		= HWID_PORAY_R50D,
		.layout_id	= "4M",
		.key		= KEY_PORAY_3,
	}, {
		.id		= "R50E",
		.hw_id		= HWID_PORAY_R50E,
		.layout_id	= "4M",
		.key		= KEY_PORAY_4,