Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion gwsettings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
*
*/

#include <boost/crc.hpp>
#include <algorithm>
#include "nonvoldef.h"
#include "gwsettings.h"
Expand Down
32 changes: 31 additions & 1 deletion profiledef.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,37 @@ struct bcm2_profile bcm2_profiles[] = {
.pretty = "Netgear CG3000",
.pssig = 0xa0f7,
.spaces = {
{ .name = "ram" },
{
.name = "ram",
.min = 0x80000000,
.size = 256 * 1024 * 1024,
.parts = {
{ "bootloader", 0x83f80000, 0x020000 },
}
},
{
.name = "nvram",
.size = 1 * 1024 * 1024,
.parts = {
{ "bootloader", 0x00000, 0x10000 },
{ "permnv", 0x10000, 0x10000, "perm" },
{ "vennv", 0x20000, 0x10000 },
{ "vennv2", 0x30000, 0x10000 },
{ "dynnv", 0xc0000, 0x40000, "dyn" },
},
},
{
.name = "flash",
.size = 128 * 1024 * 1024,
.parts = {
{ "linuxapps", 0x0000000, 0x1f40000, "image3e" },
{ "image1", 0x1f40000, 0x0900000 },
{ "image2", 0x2840000, 0x0900000 },
{ "linux", 0x3140000, 0x0480000, "image3" },
{ "linuxkfs", 0x35c0000, 0x4800000, "" },
{ "dhtml", 0x7dc0000, 0x0240000 },
}
},
},
},
{
Expand Down
62 changes: 47 additions & 15 deletions util.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@

#ifndef BCM2UTILS_UTIL_H
#define BCM2UTILS_UTIL_H
#include <boost/endian/conversion.hpp>
#include <boost/crc.hpp>
#include <system_error>
#include <type_traits>
#include <functional>
Expand All @@ -34,6 +32,7 @@
#include <chrono>
#include <memory>
#include <cerrno>
#include <cstdint>
#include <vector>
#include <string>
#include <list>
Expand Down Expand Up @@ -195,24 +194,36 @@ template<class T> T align_right(const T& num, size_t alignment)
return num + (rem ? alignment - rem : 0);
}

template<class T> typename T::value_type crc_generic(const void* buf, size_t size)
{
T crc;
crc.process_bytes(buf, size);
return crc.checksum();
}

inline uint16_t crc16_ccitt(const void* buf, size_t size)
{
return crc_generic<boost::crc_ccitt_type>(buf, size);
uint16_t crc = 0xffff;
const uint8_t *p = static_cast<const uint8_t*>(buf);

while (size--) {
crc ^= static_cast<uint16_t>(*p++) << 8;
for (int i = 0; i < 8; ++i) {
crc = (crc & 0x8000) ? (crc << 1) ^ 0x1021 : crc << 1;
}
}

return crc;
}

inline uint16_t crc16_ccitt(const std::string& buf)
{ return crc16_ccitt(buf.data(), buf.size()); }

inline uint32_t crc32(const std::string& buf)
{
return crc_generic<boost::crc_32_type>(buf.data(), buf.size());
uint32_t crc = 0xffffffff;

for (uint8_t byte : buf) {
crc ^= byte;
for (int i = 0; i < 8; ++i) {
crc = (crc & 1) ? (crc >> 1) ^ 0xedb88320 : crc >> 1;
}
}

return crc ^ 0xffffffff;
}

class mstimer
Expand Down Expand Up @@ -242,24 +253,45 @@ std::string transform(const std::string& str, std::function<int(int)> f);

std::string escape(std::string str, bool escape_quote = false);

template<class T> inline T bswap(T n)
{
typename std::make_unsigned<T>::type u = n;
typename std::make_unsigned<T>::type r = 0;

for (size_t i = 0; i < sizeof(T); ++i) {
r = (r << 8) | (u & 0xff);
u >>= 8;
}

return static_cast<T>(r);
}

template<class T> inline T be_to_h(T n)
{
return boost::endian::big_to_native(n);
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
return bswap(n);
#else
return n;
#endif
}

template<class T> inline T le_to_h(T n)
{
return boost::endian::little_to_native(n);
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
return n;
#else
return bswap(n);
#endif
}

template<class T> inline T h_to_be(T n)
{
return boost::endian::native_to_big(n);
return be_to_h(n);
}

template<class T> inline T h_to_le(T n)
{
return boost::endian::native_to_little(n);
return le_to_h(n);
}

template<typename T> using sp = std::shared_ptr<T>;
Expand Down